wpf 带有 GridView 选定行的 ListView - 删除 3D 外观

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14090113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:49:27  来源:igfitidea点击:

ListView with GridView selected row - remove 3D appearance

wpflistviewgridview

提问by Smadar Tsdaka

I have a WPF ListView that contains a GridView. I want the selected row to look "flat" and not 3d style.

我有一个包含 GridView 的 WPF ListView。我希望所选行看起来“平坦”而不是 3d 样式。

Dose anyone know how to do this? Thanks, Smadar

有谁知道如何做到这一点?谢谢,斯马达尔

回答by zmb

The 3D look is part of the default style. To change this you need to replace the ControlTemplatefor ListViewItem. Here's a simple example which produces the following: ListView screenshot

3D 外观是默认样式的一部分。要更改此设置,您需要替换ControlTemplatefor ListViewItem。这是一个简单的例子,它产生以下内容:列表视图截图

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="A"/>
                </GridView>
            </ListView.View>
            <ListView.Items>
                <ListViewItem Content="Item 1"/>
                <ListViewItem Content="Item 2"/>
                <ListViewItem Content="Item 3"/>
            </ListView.Items>

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <Border CornerRadius="2" SnapsToDevicePixels="True"
                                        BorderThickness="{TemplateBinding     BorderThickness}" 
                                        BorderBrush="{TemplateBinding BorderBrush}" 
                                        Background="{TemplateBinding Background}">
                                    <Border Name="InnerBorder" CornerRadius="1"   BorderThickness="1">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition MaxHeight="11" />
                                                <RowDefinition />
                                            </Grid.RowDefinitions>
                                            <Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF" />
                                            <GridViewRowPresenter Grid.RowSpan="2" 
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                        </Grid>
                                    </Border>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Background" Value="LightBlue"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>
</Window>

Note: The default templates are located here http://msdn.microsoft.com/en-us/library/ms788747.aspx. Since there is no way to change part of a ControlTemplateor base one off of an existing template, I usually try to keep as much of the default template as I can, and only change the parts I care about. It's a little verbose but should do what you're looking for.

注意:默认模板位于http://msdn.microsoft.com/en-us/library/ms788747.aspx。由于无法更改ControlTemplate现有模板的一部分或基础,我通常会尽量保留默认模板,只更改我关心的部分。这有点冗长,但应该做你正在寻找的。