WPF ListView样式边框

时间:2013-07-31 13:28:43

标签: wpf vb.net visual-studio-2012

今天我开始教自己如何创建一个wpf应用程序(最后)

所以我开始习惯它,但是我在设计列表视图方面遇到了一些麻烦。

在这张顶部图片中,您可以看到它完美运行(选择周围没有边框)

enter image description here

然而,在第二张图片中你可以看到选区周围有一个小边框。看来这只发生在我用键盘转到列表中的下一个项目时。

样式中是否有我缺少的东西,所以我可以摆脱这个边界?

代码:(记得我今天才开始学习,所以它可能很乱)

列表视图样式

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

2 个答案:

答案 0 :(得分:1)

抱歉,如果我在某处工作了网格视图,这会更容易 我的第二个想法是:ListViewItem有一个FocusVisualStyle。您可以按照自己的风格设置:

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

(或者您将其设置为null,因为它在此处完成: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>

答案 1 :(得分:0)

请以<Setter Property="Control.Padding" Value="0" />

的格式尝试ListViewItem