2017-10-31 17 views
0

ContentPressenter를 올바른 위치에 만들려면 AlternationIndex를 사용하여 Grid.Row 값을 설정하려고합니다. 하지만 ContentPresenter의 ItemsControl.AlternationIndex를 스타일에 바인딩 할 수없는 것 같습니다. 스타일 트리거를 통해 ItemsControl.AlternationIndex에 액세스 할 수 있지만 바인딩을 통해 액세스 할 수 없습니다. ContentPresenter에 ItemsControl.AlternationIndex에 액세스하는 방법에 대한 아이디어가 있으십니까? 다음 코드는이 질문에 대한 간단한 설명입니다.ItemsControl에서 AlternationIndex를 사용하여 ContentPresenter Grid.Row 인덱스를 설정할 수 없습니다.

MainWindow.xaml

<Window x:Class="ContentPresenterIndex.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:ContentPresenterIndex" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ItemsControl ItemsSource="{Binding NameList}" AlternationCount="{Binding NameList.Count}"> 
      <ItemsControl.ItemContainerStyle> 
       <Style TargetType="{x:Type ContentPresenter}"> 
        <Setter Property="Grid.Row" Value="{Binding ItemsControl.AlternationIndex, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"/> 
       <Setter Property="Grid.Row" Value="{Binding ItemsControl.AlternationIndex, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"/> 
       <!--<Style.Triggers> <----- this is ok 
        <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
         <Setter Property="Grid.Row" Value="1"/> 
        </Trigger> 
       </Style.Triggers>--> 
      </Style> 
       </Style> 
      </ItemsControl.ItemContainerStyle> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <Grid IsItemsHost="True"> 
         <Grid.RowDefinitions> 
          <RowDefinition/> 
          <RowDefinition/> 
          <RowDefinition/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
        </Grid> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition/> 
         </Grid.ColumnDefinitions> 
         <TextBlock Text="{Binding Name}" Grid.Column="0"/> 
         <TextBlock Text="{Binding Age}" Grid.Column="1"/> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 
</Window> 

MainWindow.xaml.cs를

namespace ContentPresenterIndex 
{ 
    public class NameItem 
    { 
     public string Name { get; set; } 
     public int Age { get; set; } 
    } 

    public partial class MainWindow : Window 
    { 
     public ObservableCollection<NameItem> NameList { get; set; } 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = this; 

      NameList = new ObservableCollection<NameItem> 
      { 
       new NameItem { Name = "John", Age = 20 }, 
       new NameItem { Name = "Jane", Age = 21 }, 
       new NameItem { Name = "Davie", Age = 22 }, 
       new NameItem { Name = "Robert", Age = 23 } 
      }; 
     } 
    } 
} 

답변

1
<ItemsControl.ItemContainerStyle> 
       <Style TargetType="{x:Type ContentPresenter}"> 
        <Setter Property="Grid.Row" Value="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" /> 
       </Style> 
      </ItemsControl.ItemContainerStyle>