【问题标题】:Change ListViewItem background color by using an index specified by a property使用属性指定的索引更改 ListViewItem 背景颜色
【发布时间】:2019-07-05 07:11:15
【问题描述】:

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

【问题讨论】:

  • ItemsControl.AlternationIndex 是所谓的附加属性。它不是 ItemsControl 本身的属性,而是 ItemsControl (docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/…) 附加到某个项目的属性。因此,您的 AlternationIndex 绑定不正确。尝试绑定 ListViewItem 本身,使用"(ItemsControl.AlternationIndex)" 作为绑定路径。 (括号可能是也可能不是必要的;我不记得了。)
  • @elgozo &lt;Binding Path="ItemsControl.AlternationIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}"/&gt; 我试过了,但我得到一个错误,“找不到与参考绑定的源...”

标签: c# wpf datatrigger


【解决方案1】:

正如@elgonzo 所指出的,ItemsControl.AlternationIndex 是一个attached property,您应该像这样绑定:

<Binding Path="(ItemsControl.AlternationIndex)" 
         RelativeSource="{RelativeSource Self}"/>

注意路径周围的括号。

【讨论】:

  • 我试过了,但我得到了System.Windows.Data Warning: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListViewItem', AncestorLevel='1''. BindingExpression:Path=(0); DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'NoTarget' (type 'Object')
  • 在转换器中我得到一个InvalidCastException
  • @FreddxL.:查看我的编辑。既然你在ItemContainerStyle,你应该指定{RelativeSource Self}作为源。
  • 谢谢,现在正在工作,但只是改变位置 0 的 ListViewItem 的颜色,原因可能是转换器,我会检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 2012-12-07
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 2014-11-21
相关资源
最近更新 更多