2012-07-24 77 views
0

在ItemsControl的索引I有一個簡單的類它創建的對象的列表:綁定到從的DataTemplate

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. 

這是非常直接的綁定到列表中的每個項目的名字屬性,但我也想綁定到列表中的指數。我知道我可以使用ItemsControl.AlternationIndex在ItemsControl中執行此操作,但是如何鏈接到DataTemplate中的AlternationIndex?

+0

你有沒有考慮一個的RelativeSource約束力?它不是最好的方式,但我在這種情況下工作。還要記住,AlternationIndex不是列表中的索引,而更像是當前索引的模數。例如,您可以使用它爲網格中的每第二行着色。 – dowhilefor 2012-07-24 10:36:51

+0

嗨,對不起,以上是我的代碼的精簡版。我將ItemsControl上的AlternationCount屬性設置爲適當的高數值,以便它不會'換行'。我試過使用RelativeSource,但迄今爲止沒有運氣! – JamesPD 2012-07-24 12:46:54

回答

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> 

希望這有助於..