无法使用 ItemsControl 中的 AlternationIndex 设置 ContentPresenter Grid.Row 索引
Unable to set ContentPresenter Grid.Row Index using AlternationIndex in ItemsControl
我正在尝试使用 AlternationIndex 设置 Grid.Row 值以使 ContentPressenter 处于正确的位置。
但它似乎无法在样式上为 ContentPresenter 绑定 ItemsControl.AlternationIndex。
我可以通过样式触发器访问 ItemsControl.AlternationIndex,但无法通过绑定访问。
关于如何为 ContentPresenter 访问 ItemsControl.AlternationIndex 有什么想法吗?
下面的代码是对这个问题的简单演示。
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 }
};
}
}
}
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Row" Value="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" />
</Style>
</ItemsControl.ItemContainerStyle>
我正在尝试使用 AlternationIndex 设置 Grid.Row 值以使 ContentPressenter 处于正确的位置。 但它似乎无法在样式上为 ContentPresenter 绑定 ItemsControl.AlternationIndex。 我可以通过样式触发器访问 ItemsControl.AlternationIndex,但无法通过绑定访问。 关于如何为 ContentPresenter 访问 ItemsControl.AlternationIndex 有什么想法吗? 下面的代码是对这个问题的简单演示。
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 }
};
}
}
}
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Row" Value="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" />
</Style>
</ItemsControl.ItemContainerStyle>