0

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

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 属性非常简单,但我也想绑定到列表中的索引。我知道我可以使用 ItemsControl.AlternationIndex 从 ItemsControl 内部执行此操作,但是如何从 DataTemplate 内部链接到 AlternationIndex?

4

1 回答 1

0

您需要了解您的上下文TestObj和触发器,您基本上是在检查一个名为的属性的值,该属性ItemsControl应该有一个 property 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>

希望这可以帮助..

于 2012-07-24T13:09:15.990 回答