使用wpf MVVM中的数据模板进行选项卡控制

时间:2013-11-18 12:06:04

标签: wpf mvvm tabcontrol

我想根据案例列表加载标签数量,每个点击都要显示案例的详细信息。一个案例也会有文本和列表集合。 以下是我的代码无法加载内容                                                

                    <CheckBox Content="Tab  " d:LayoutOverrides="Width, Height" FontFamily="{StaticResource BoldFontFamily}" FontWeight="Bold" FontSize="14.667"/>
                </DataTemplate>
            </TabControl.ItemTemplate>

            <TabControl.ContentTemplate>

                <DataTemplate x:Name="ContentTemplate1">
                    <Grid Margin="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="100"/>
                            <RowDefinition Height="230"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="{Binding ID}"></TextBlock>
                        <ListBox x:Name="lbItems" ItemsSource="{Binding ItemList}" Grid.Row="1" MinHeight="100" Style="{StaticResource PT_ListBoxStyle}" 
             ItemContainerStyle="{StaticResource ListBoxItemStyle}" >
                        <ListBox.Resources>
                            <DataTemplate x:Key="DataTemplate">
                                <Grid d:DesignWidth="623.375" Margin="0,5">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="160"/>
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Grid.Column="0" TextWrapping="Wrap" VerticalAlignment="Stretch" FontFamily="{StaticResource RegularFontFamily}" FontSize="21.333" Text="{Binding IText}" Width="721" HorizontalAlignment="Left"/>
                                    <TextBlock Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" FontFamily="{StaticResource RegularFontFamily}" FontSize="21.333" Width="Auto" Text="{Binding IType}"/>
                                    <TextBlock Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" FontFamily="{StaticResource RegularFontFamily}" FontSize="21.333" d:LayoutOverrides="Width, Height" Margin="10,0,0,0" TextAlignment="Right" Text="{Binding IAmount}"/>
                                </Grid>
                            </DataTemplate>
                        </ListBox.Resources>
                    </ListBox>
                 </Grid>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>

上面是我的XML代码和...在我的视图模型中,

 private List<Case> _cases = new List<Case>(); 
   public List<Case> Cases { get { return _cases; } set { _cases = value; } }
public void GetList() 
 { 
 List<Case> cases = Customer.GetTabDetails(date); 
  Cases=cases; 
 } 

在视图模型代码上方。案例类具有ID,项目列表,客户列表等属性

有人可以为此提出解决方案吗?

1 个答案:

答案 0 :(得分:0)

将模板明确定义为ListBox s ItemTemplate而不是动态资源。我会尽可能避免动态资源!

<TabControl.ContentTemplate>
    <DataTemplate x:Name="ContentTemplate1">
        <ListBox x:Name="lbItems" 
                 ItemsSource="{Binding ItemList}" 
                 Style="{StaticResource PT_ListBoxStyle}" 
                 ItemContainerStyle="{StaticResource ListBoxItemStyle}"      
                 MinHeight="100">
                    <ListBox.ItemTemplate>
                        <DataTemplate >
                            <Grid d:DesignWidth="623.375" Margin="0,5">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="160"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" TextWrapping="Wrap" VerticalAlignment="Stretch" FontFamily="{StaticResource RegularFontFamily}" FontSize="21.333" Text="{Binding IText}" Width="721" HorizontalAlignment="Left"/>
                                <TextBlock Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" FontFamily="{StaticResource RegularFontFamily}" FontSize="21.333" Width="Auto" Text="{Binding IType}"/>
                                <TextBlock Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" FontFamily="{StaticResource RegularFontFamily}" FontSize="21.333" d:LayoutOverrides="Width, Height" Margin="10,0,0,0" TextAlignment="Right" Text="{Binding IAmount}"/>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>