WPF ListView 样式边框 [英] WPF ListView Style Borders
问题描述
今天我开始自学如何创建一个 wpf 应用程序(终于)
Today i started teaching my self how to create a wpf application (finally)
所以我开始习惯它,但我在为列表视图设置样式时遇到了一些障碍.
So i am starting to get used to it but i have hit a bit of a snag with styling a listview.
在这张顶部图片中,您可以看到它运行良好(选区周围没有边框)
In this top picture you can see it working perfectly (no border around the selection)
但是在第二张图片中,您可以看到所选内容周围有一个小边框.. 似乎只有当我使用键盘转到列表中的下一项时才会发生这种情况.
However in the second picture you can see a small border around the selection.. It seems this only happens when i use the keyboard to go to the next item in the list.
样式中是否缺少某些东西以便我可以摆脱此边框?
Is there something i am missing in the styles so i can get rid of this border?
代码:(记得我今天才开始学习所以可能很乱)
Code: (remember i only started learning today so it is probably messy)
列表视图样式
<Style TargetType="{x:Type ListView}">
<Setter Property="BorderThickness" Value="0" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderThickness" Value="0" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="#4b0037" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
BorderBrush="Transparent"
BorderThickness="0"
Background="{TemplateBinding Background}">
<GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="images/selection.png"/>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="images/selection.png"/>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="#FFFFFF" />
</Trigger>
</Style.Triggers>
</Style>
列表视图 XAML
<ListView Background="transparent" Margin="10 10 10 10" x:Name="Mylist" HorizontalAlignment="Stretch" VerticalContentAlignment="center" VerticalAlignment="Stretch" BorderBrush="Transparent">
<ListView.View>
<GridView>
<GridViewColumn Header="img" Width="150" DisplayMemberBinding="{Binding img}" />
<GridViewColumn Header="name" Width="150" DisplayMemberBinding="{Binding name}" />
<GridViewColumn Header="path" Width="150" DisplayMemberBinding="{Binding path}" />
</GridView>
</ListView.View>
</ListView>
推荐答案
抱歉,如果我在某处使用网格视图,这会更容易.
我的第二个想法是:ListViewItem
s 有一个 FocusVisualStyle
.您可以按照自己的风格进行设置:
Sorry, this would be easier if I had a grid view working somewhere.
My second idea is this: ListViewItem
s have a FocusVisualStyle
. You can set it like this in your style:
<Setter Property="ListViewItem.FocusVisualStyle" Value="{DynamicResource ListViewItemFocusVisualStyle}" />
(或者您将其设置为 null
,因为它在这里完成:GridView 模式下的 WPF ListView 高亮问题)
(Or you set it to null
as it is done here: WPF ListView in GridView mode Highlighting problem)
样式可能如下所示:
<Style.Resources>
<Style x:Key="ListViewItemFocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="4, 1" StrokeThickness="1"
Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
StrokeDashArray="1 2"
SnapsToDevicePixels="True" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
这篇关于WPF ListView 样式边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!