WPF ListView Style Borders

。_饼干妹妹 提交于 2019-12-13 06:13:10

问题


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)

Listview Styles

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

Listview 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>

回答1:


Sorry, this would be easier if I had a grid view working somewhere.
My second idea is this: ListViewItems have a FocusVisualStyle. You can set it like this in your style:

<Setter Property="ListViewItem.FocusVisualStyle" Value="{DynamicResource ListViewItemFocusVisualStyle}" />

(Or you set it to null as it is done here: WPF ListView in GridView mode Highlighting problem)

The style could look like this:

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



回答2:


Please try <Setter Property="Control.Padding" Value="0" /> in your style for the ListViewItem



来源:https://stackoverflow.com/questions/17971771/wpf-listview-style-borders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!