简体   繁体   中英

Bind to index in ItemsControl from DataTemplate

I have a simple class which creates a list of objects:

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();
    }
  }
}

The TestObj class is as follows:

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

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

I then attempt to display each item in the list with the following:

<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>

What I would like to display is something like:

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

It's pretty straight-forward to bind to the FirstName property of each item in the list, but I would also like bind to the index in the list. I know I can do this from inside an ItemsControl using ItemsControl.AlternationIndex, but how do I link to the AlternationIndex from within in DataTemplate?

You need to understand that your context is TestObj and with your trigger, you are basicly checking the value of a property named ItemsControl which should have a property AlternationIndex .

You should changed the context of your triggers with a RelativeSource binding to the control that hold your object, named the 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>

Hope this helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM