【问题标题】:Different DataTemplates for ItemsControl WPFItemsControl WPF的不同数据模板
【发布时间】:2015-06-04 18:03:19
【问题描述】:

我在 XAML 中将 ItemsControl 定义为:

<ItemsControl ItemsSource="{Binding MyCollection}"
              AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}">

        <ItemsControl.Resources>
            <DataTemplate x:Key="TemplateOne">
                <Button Content="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleOne}"/>
            </DataTemplate>
            <DataTemplate x:Key="TemplateTwo">
                <Button Content="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleTwo}"/>
            </DataTemplate>
        </ItemsControl.Resources>

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="ContentTemplate" Value="{StaticResource TemplateOne}"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="ContentTemplate" Value="{StaticResource TemplateTwo}"></Setter>
                    </Trigger>
                </Style.Triggers>                    
            </Style>
        </ItemsControl.ItemContainerStyle>           

    </ItemsControl>

我可以根据 ItemsControl 的当前交替索引设置不同的模板。虽然这有效并为我提供了不同的数据模板,但我还希望 Button 的内容显示其交替索引,即 MyCollection 中项目的索引。有什么想法我可能会出错吗?

【问题讨论】:

    标签: wpf itemscontrol


    【解决方案1】:

    试试

    <Button Content="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter},
                              Path=(ItemsControl.AlternationIndex)}"
    

    因为ItemsControl.AlternationIndex 生活在ItemContainer(这是ContentPresenter)而不是Button(在您的示例中)。

    【讨论】:

      【解决方案2】:

      您没有为所有项目提供替代样式。你只有两个。我将 ItemsControl Count 设置为 2。 所以这里是你的 ItemsControl(它包括@LPL 的解决方案)

          <ItemsControl ItemsSource="{Binding MyCollection}"
                AlternationCount="2">
      
              <ItemsControl.Resources>
                  <DataTemplate x:Key="TemplateOne">
                      <Button Content="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleOne}"/>
                  </DataTemplate>
                  <DataTemplate x:Key="TemplateTwo">
                      <Button Content="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleTwo}"/>
                  </DataTemplate>
              </ItemsControl.Resources>
      

      ItemsControl AlternationCount on MSDN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-23
        • 2013-06-15
        • 1970-01-01
        • 1970-01-01
        • 2018-09-27
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        相关资源
        最近更新 更多