我正在玩 wpf,我看到了以下文章: WPF ListView Inactive Selection Color
我想做类似的事情。我想在选择列表视图项时在它周围放置一个边框,并且我不想更改背景颜色。我想要这个的原因是我想要一个颜色编码的列表视图,并且我仍然想在选择它时看到它的颜色,但我想知道它是通过它周围有边框来选择的。
有任何想法吗?
更新:
我尝试了下面的答案,它让我成功了一半,它确实在 listviewitem 周围设置了一个边框,但它覆盖了我的背景颜色。我无法得到我尝试过的正确语法(注意 BasedOn):
<Style x:Key="SourceListView" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/>
</Style>
<Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource SourceListView}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
x:Name="Border"
BorderBrush="Transparent"
BorderThickness="1">
<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>
然后我尝试了这个:
<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>
两次尝试都只是将背景设置为白色(或我不知道的透明)。我知道这只是语法,我很感激朝着正确的方向再次轻推:)