使用 属性 指定的索引更改 ListViewItem 背景颜色
Change ListViewItem background color by using an index specified by a property
我是 WPF 的新手,我有一个 属性,它定义了一个 int
,它是元素的索引,用于更改 ListView
中的背景颜色,但正在更改所有 ListViewItem
的背景颜色,这里是我的代码:
<ListView AlternationCount="2147483647" x:Name="listView1" HorizontalAlignment="Left" Height="295" Margin="10,10,0,0" VerticalAlignment="Top" Width="380" Padding="0" HorizontalContentAlignment="Center" BorderThickness="1" IsSynchronizedWithCurrentItem="False" SelectionMode="Single" ScrollViewer.CanContentScroll="True">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource ListViewItemContexMenuKey}"/>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IndexConverter}">
<Binding Path="AlternationIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}"/>
<Binding Path="TargetIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="#FFFF7171"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
这里是 IndexConverter
public class IndexConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
int index = (int)values[0];
int targetIndex = (int)values[1];
Console.WriteLine($"Index: {index}, TargetIndex: {targetIndex}");
return index == targetIndex;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在 IndexConverter.Convert
中,局部变量索引是 AlternationIndex
并且始终返回 0。
我不明白为什么会出现这种行为,当使用触发器时它可以正常工作,但您不能使用触发器进行绑定。
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFFF7171"/>
</Trigger>
正如@elgonzo 所指出的,ItemsControl.AlternationIndex
是一个 attached property,您应该像这样绑定它:
<Binding Path="(ItemsControl.AlternationIndex)"
RelativeSource="{RelativeSource Self}"/>
注意路径周围的括号。
我是 WPF 的新手,我有一个 属性,它定义了一个 int
,它是元素的索引,用于更改 ListView
中的背景颜色,但正在更改所有 ListViewItem
的背景颜色,这里是我的代码:
<ListView AlternationCount="2147483647" x:Name="listView1" HorizontalAlignment="Left" Height="295" Margin="10,10,0,0" VerticalAlignment="Top" Width="380" Padding="0" HorizontalContentAlignment="Center" BorderThickness="1" IsSynchronizedWithCurrentItem="False" SelectionMode="Single" ScrollViewer.CanContentScroll="True">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource ListViewItemContexMenuKey}"/>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IndexConverter}">
<Binding Path="AlternationIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}"/>
<Binding Path="TargetIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="#FFFF7171"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
这里是 IndexConverter
public class IndexConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
int index = (int)values[0];
int targetIndex = (int)values[1];
Console.WriteLine($"Index: {index}, TargetIndex: {targetIndex}");
return index == targetIndex;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在 IndexConverter.Convert
中,局部变量索引是 AlternationIndex
并且始终返回 0。
我不明白为什么会出现这种行为,当使用触发器时它可以正常工作,但您不能使用触发器进行绑定。
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFFF7171"/>
</Trigger>
正如@elgonzo 所指出的,ItemsControl.AlternationIndex
是一个 attached property,您应该像这样绑定它:
<Binding Path="(ItemsControl.AlternationIndex)"
RelativeSource="{RelativeSource Self}"/>
注意路径周围的括号。