【发布时间】:2011-10-13 23:05:48
【问题描述】:
我在 ListView 上使用(3.5 版的新功能)AlternationIndex 触发器来应用这样的交替背景颜色:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="White" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#300761AE" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
这部分效果很好,ListView 看起来完全符合我的要求。我唯一的问题是 SelectedItem 的背景颜色。对于我的列表,实际上无法选择项目,因为从上下文来看这是没有意义的。我想要找到的是一种让 SelectedItem 的背景颜色变为空的方法,这样父 ListViewItem 的颜色就会发光。透明不起作用,因为这基本上等同于白色,并且当我使用透明时交替样式消失了。它也不允许我将其设置为 null。
我知道我需要更改 SystemColors.HighlightBrushKey,但我不知道如何设置绑定来查找父 ListViewItem 的背景颜色。这是尝试:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=Background}" />
这可能吗?
【问题讨论】:
标签: wpf listview background-color