Wpf 列表视图选择颜色
我正在玩wpf,看到了以下文章: 我想做类似的事情。我想在listviewitem被选中时在其周围放置一个边框,并且不想更改背景色。我想要这个的原因是我想要一个颜色编码的listview,我仍然想看到它被选中时的颜色,但我想知道它被选中时周围有一个边框 有什么想法吗 更新: 我尝试了下面的答案,它让我半途而废,它确实在listviewitem周围放置了一个边框,但它覆盖了我的背景色。我无法获得我尝试过的正确语法(请注意BasedOn):Wpf 列表视图选择颜色,wpf,listview,selection,listviewitem,Wpf,Listview,Selection,Listviewitem,我正在玩wpf,看到了以下文章: 我想做类似的事情。我想在listviewitem被选中时在其周围放置一个边框,并且不想更改背景色。我想要这个的原因是我想要一个颜色编码的listview,我仍然想看到它被选中时的颜色,但我想知道它被选中时周围有一个边框 有什么想法吗 更新: 我尝试了下面的答案,它让我半途而废,它确实在listviewitem周围放置了一个边框,但它覆盖了我的背景色。我无法获得我尝试过的正确语法(请注意BasedOn): 然后我试了一下: <Style x:K
然后我试了一下:
<Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/>
<Setter Property="Template">
...//Same as above
</Setter>
</Style>
…//同上
两次尝试都只是将背景设置为白色(或透明,我不知道)。我知道这只是语法问题,我希望能朝着正确的方向再推一次:)将ListView上的ItemContainerStyle更改为一种样式,该样式在选中某个项目时不会更改背景,而是更改边框的颜色。以下是一个例子:
<Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
x:Name="Border"
BorderBrush="Transparent"
BorderThickness="1"
Background="{TemplateBinding Background}">
<GridViewRowPresenter Columns="{TemplateBinding GridView.ColumnCollection}" Content="{TemplateBinding Content}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后使用如下样式:
<ListView ItemContainerStyle="{StaticResource MyListViewItemStyle}">
...
</ListView>
...
我尝试了这一点,它确实设置了边框,但我的背景色语法似乎被覆盖了。还要帮忙吗?我注意到的另一件事是,只有在使用上述样式单击listviewitem的非空白区域(例如,单击列的内容)时,listview才会被选中。为什么会这样?我更新了我的代码示例。我在ListViewItem的背景属性中添加了一个TemplateBinding到border的背景属性中。我不知道为什么它的行为就像你在解释选择。我已经尝试了我的例子,我可以点击一行上的空白,仍然选择它。
<ListView ItemContainerStyle="{StaticResource MyListViewItemStyle}">
...
</ListView>