ListView with GridView selected row - remove 3D appearance

后端 未结 1 1585
南笙
南笙 2021-01-06 23:45

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

Dose anyone know how to do this? Thanks, Smadar

相关标签:
1条回答
  • 2021-01-07 00:31

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

    <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 ControlTemplate or 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.

    0 讨论(0)
提交回复
热议问题