【问题标题】:ItemsControl.ItemTemplate with multiple DataTemplate具有多个 DataTemplate 的 ItemsControl.ItemTemplate
【发布时间】:2013-10-22 10:00:57
【问题描述】:

我有一个可以水平滚动页面的 FlipVIew。 每个页面都包含一个带有 ItemsControl 的 ScrollViewer(垂直滚动)。 itemsControl 包含一个 itemTemplate,它是一行(每个页面包含数据行)。

I want to change the template of a row at some button click。现在我有 1 种类型的行,但我想实现另外 2 种类型,但不知道如何......基本上当前行类型是通过 DataTemplate 定义的,我想定义另外 2 个 DataTemplate 并将 dataTemplate 绑定到 ItemsControl。项目模板

<ItemsControl x:Name="RowItemsControl" ItemsSource="{Binding OptionItems, Mode=OneWay}" Visibility="{Binding OptionsPageVisibility}">
      <ItemsControl.ItemTemplate>
           <DataTemplate x:Name="RowType1">
               <Grid x:Name="OptionItemGrid" Background="White" HorizontalAlignment="Stretch">
                     <Grid.RowDefinitions>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                      </Grid.RowDefinitions>
                      <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                       </Grid.ColumnDefinitions>
                              <!-- here is the content of the rowType1 -->                  
                 </Grid>
             </DataTemplate>
            <!--<DataTemplate x:Name="RowType2">  --- I want just 1 of these 3 data to be my item template
            </DataTemplate x:Name="RowType2">
            <DataTemplate x:Name="RowType3">
            </DataTemplate x:Name="RowType3"> -->
        </ItemsControl.ItemTemplate>
   </ItemsControl>

【问题讨论】:

  • RowItemsControl 声明在哪里,哪一部分你不知道怎么做?
  • RowItemsControl 是一个 ItemsControl - 它绑定到一些包含数据数组的 ObservableCollection。现在我在 中有 1 个 DataTemplate。如何定义另外 2 个 DataTemplates(当然还有其他外观)并选择这 3 个 dataTemplates 中的 1 个作为 的内容?
  • @Sheridan - 请再次查看我的帖子(我在其中进行了一些编辑(在 xaml 中))
  • 我希望每个 DataTemplate 都有它的 Gui 并且当用户按下按钮来更改数据模板(行的 gui)

标签: wpf xaml windows-store-apps


【解决方案1】:

首先,您需要在Resources 部分定义您的DataTemplate 对象...甚至可以使用您的ItemsControl.Resources

<ItemsControl x:Name="RowItemsControl" ItemsSource="{Binding OptionItems, Mode=OneWay}" Visibility="{Binding OptionsPageVisibility}">
      <ItemsControl.Resources>
           <DataTemplate x:Name="RowType1">
               <Grid x:Name="OptionItemGrid" Background="White" HorizontalAlignment="Stretch">
                     <Grid.RowDefinitions>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                      </Grid.RowDefinitions>
                      <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                          <ColumnDefinition Width="*" />
                       </Grid.ColumnDefinitions>
                 </Grid>
             </DataTemplate>
            <DataTemplate x:Name="RowType2">
                ...
            </DataTemplate x:Name="RowType2">
            <DataTemplate x:Name="RowType3">
                ...
            </DataTemplate x:Name="RowType3">
        </ItemsControl.Resources>
   </ItemsControl>

接下来,您需要从Resources 访问和设置它,可能在Button.Click 处理程序中:

private void Button_Click(object sender, RoutedEventArgs e)
{
    DataTemplate rowType2DataTemplate = RowItemsControl.FindResource("RowType2") as 
        DataTemplate;
    if (rowType2DataTemplate != null) RowItemsControl.ItemTemplate = 
        rowType2DataTemplate;
}

这应该可以解决问题...如果您有任何问题,请告诉我。

【讨论】:

  • 谢谢,我正在阅读 UserControl.Resources 的东西...这对我来说非常明确...
  • WPF 有一个陡峭的学习曲线,因为它是如此不同。但是,绝对值得坚持...继续阅读。 :)
猜你喜欢
  • 2016-08-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多