【问题标题】:ListView Selected Item Style OverrideListView 选定项样式覆盖
【发布时间】:2017-02-15 04:59:02
【问题描述】:

我有一个 WPF ListView,我也在尝试应用自定义样式...主要是我想让 listview 框的背景透明且没有边框...我想覆盖突出显示和选定的项目样式。默认高亮为半透明蓝色,选中项目为灰色。我想覆盖这些以自定义它们。我已经关注了几个线程和教程......包括这个one 但是,我的突出显示和选定的项目保持默认样式。这就是我所拥有的。

<UserControl.Resources>
    <ResourceDictionary>
        <Style x:Key="ListViewStyle" TargetType="ListView">
            <Setter Property="Background" Value="Transparent"></Setter>
            <Setter Property="BorderThickness" Value="0"></Setter>
            <Setter Property="BorderBrush" Value="Transparent"></Setter>
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
            </Style.Resources>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

....

<ListView Style="{StaticResource ListViewStyle}" ItemsSource="{Binding Path=Items}" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectedItem="{Binding Path=DataContext.Current" >
    <ListView.ItemTemplate>
        <DataTemplate DataType="local:Cases">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                <!-- Some UI Controls Here  -->
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    这种尝试覆盖系统颜色的方法不适用于 Windows 8 及更高版本。

    您需要修改ListViewItem容器的ControlTemplate

    您可以通过在 Visual Studio 或 Blend 中的设计模式下右键单击 ListView 并选择 Edit Additional Templates->Edit Generated Item Container (ItemContainerStyle) 将默认模板复制到 XAML 标记中,然后将其编辑为根据您的要求,例如更改SolidColorBrushes 的Color 属性:

    <ListView ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Path=DataContext.Current" >
        <ListView.Resources>
            <Style x:Key="FocusVisual">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <SolidColorBrush x:Key="Item.MouseOver.Background" Color="Red"/>
            <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
            <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/>
            <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/>
            <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
            <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Padding" Value="4,1"/>
                <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsMouseOver" Value="True"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
                                    <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                        <Condition Property="IsSelected" Value="True"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
                                    <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="True"/>
                                        <Condition Property="IsSelected" Value="True"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
                                    <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>
        <ListView.ItemTemplate>
            <DataTemplate DataType="local:Cases">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                    <!-- Some UI Controls Here  -->
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    【讨论】:

    • 谢谢mm8。很好的解释。有没有办法将 listview 资源分配给这个特定的列表?或者它是否应该自动覆盖样式,因为它位于 ListView 标记内?我问的原因是,当我使用上面的代码时,我没有对样式进行任何更改(即使我更改了 SolidColorBrush 值)。
    • 它应该自动覆盖默认模板,前提是您将 ListViewItem 样式放在 中并且 不要 为样式指定 x:Key。您还可以将 ListView 的 ItemContainerStyle 属性设置为自定义/覆盖的 ListViewItem 样式。
    • 你能澄清一下“不要为样式指定 x:Key”吗?如果我没看错,你的意思是我应该从 标记下的
    • 不,只是来自 ListViewItem 样式。尝试复制我提供的示例标记。确保您没有设置 ListView 的 Style 属性。
    猜你喜欢
    • 2017-10-24
    • 2020-03-04
    • 2018-09-01
    • 2020-08-10
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多