我有一个窗口,其中显示了一个列表视图。我为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选定颜色。