从DataTemplate绑定到ItemsControl中的索引

时间:2012-07-24 10:32:58

标签: c# .net wpf datatemplate itemscontrol

我有一个创建对象列表的简单类:

namespace TestWPF2
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public ObservableCollection<TestObj> SomeList { get; set; }
    public string WindowTitle { get; set; }

    public MainWindow()
    {
      this.DataContext = this;
      WindowTitle = "People";
      SomeList = new ObservableCollection<TestObj>();
      SomeList.Add(new TestObj("Bob"));
      SomeList.Add(new TestObj("Jane"));
      SomeList.Add(new TestObj("Mike"));
      InitializeComponent();
    }
  }
}

TestObj类如下:

namespace TestWPF2
{
  public class TestObj
  {
    public string FirstName { get; set; }

    public TestObj(string firstName)
    {
      this.FirstName = firstName;
    }
  }
}

然后我尝试使用以下内容显示列表中的每个项目:

<Window x:Class="TestWPF2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWPF2"    
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:TestObj}">
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Pos: "/>
                    <TextBlock x:Name="posText"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Name: "/>
                    <TextBlock Text="{Binding FirstName}"/>
                </StackPanel>
            </StackPanel>

            <!-- THESE TRIGGERS DONT WORK -->

            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Text" Value="First" TargetName="posText"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Text" Value="Second" TargetName="posText"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="3">
                    <Setter Property="Text" Value="Third" TargetName="posText"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="{Binding Title}"/>
        <ItemsControl HorizontalAlignment="Stretch"
                      ItemsSource="{Binding SomeList}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
</Window>

我想要展示的是:

Pos: First
Name: Bob
Pos: Second
Name: Jane
etc.

绑定到列表中每个项的FirstName属性非常简单,但我还想绑定到列表中的 index 。我知道我可以使用ItemsControl.AlternationIndex在ItemsControl中执行此操作,但是如何从DataTemplate中的内部链接到AlternationIndex?

1 个答案:

答案 0 :(得分:0)

您需要了解您的上下文是TestObj,并且使用触发器,您基本上会检查名为ItemsControl的属性的值,该属性应具有属性AlternationIndex

您应该使用RelativeSource绑定将触发器的上下文更改为保存对象的控件,名为ContentPresenter

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)" Value="0">
        <Setter Property="Text" Value="First" TargetName="posText"/>
    </Trigger>

    <!--- here be the other triggers -->

</DataTemplate.Triggers>

希望这会有所帮助..