ResourceDictionary中的ListViewItem样式

4
我有一个窗口,其中显示了一个列表视图。我为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">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                        <Style.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True" />
                                    <Condition Property="IsSelected" Value="False"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Red" />
                            </MultiTrigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="Blue" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <DockPanel>
        <ListView x:Name="listView"/>
    </DockPanel>
</Window>

代码背后的定义非常简单:

        public MainWindow()
        {
            InitializeComponent();
            for (int i = 1; i <= 100; i++)
            {
                listView.Items.Add(i);
            }
        }

现在,当我运行应用程序时,除了列表视图中的第一个项目外,一切看起来都很好。对于第一个项目,没有任何样式被应用。如果我更改我的XAML如下所示,删除与资源字典相关的行,则所有内容都可以正确工作:

<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">
    <Window.Resources>
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver" Value="True" />
                        <Condition Property="IsSelected" Value="False"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="Red" />
                </MultiTrigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <DockPanel>
        <ListView x:Name="listView"/>
    </DockPanel>
</Window>

我在网上找到了一些示例代码,但在几次编辑之后,这些资源字典行仍然存在,然而如果它们存在,则视图中仅第一个项目才能采用定义的样式,我不明白为什么会这样。

有什么想法吗?

编辑:

我发现我的IsSelected背景颜色没有被选中。例如,如果我将其设置为绿色,则所选项目仍使用默认的Windows选定颜色。

1个回答

2

之前遇到过这个问题,显然必须使用ListViewItem的选中背景色进行设置

<Style.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
</Style.Resources>

我从来没有理解那个原因...也许这里有其他人可以解释一下。

至于第一部分,如果你将该样式放在单独的资源字典中,就像这样。 (我也不知道为什么会得到你得到的效果)

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
        </Style.Resources>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True" />
                    <Condition Property="IsSelected" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="Red" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

主窗口

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<DockPanel Name="c_dockPanel">
    <ListView x:Name="listView"/>
</DockPanel>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接