Mar*_*ner 17 wpf styles background
我试图改变Background属性我ListBoxItem使用触发器s中ItemContainerStyle的我ListBox,如下所示:
<ListBox Height="100" HorizontalAlignment="Left" Margin="107,59,0,0" Name="listBox1" VerticalAlignment="Top" Width="239">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Lightblue"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Items>
<ListBoxItem Content="First Item"/>
<ListBoxItem Content="SecondItem"/>
<ListBoxItem Content="Third Item"/>
</ListBox.Items>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我希望未选择的项目具有浅蓝色背景,悬停项目(即当鼠标光标在它们上方时)为黄色并且所选项目为红色.
对于未选择和悬停的项目,这是按预期工作的,但所选项目仍然具有其标准背景颜色(即蓝色,如果列表框具有焦点,否则为浅灰色).
有什么我想念的吗?这种行为是否记录在某处?
谢谢你的提示!
编辑
我知道覆盖默认系统颜色的解决方案(如更改选定和未聚焦的列表框样式中所述,不会显示为灰色,非常感谢每个人都将此作为答案发布).然而,这不是我想要做的.我更感兴趣的是为什么我的解决方案不起作用.
我怀疑标准ControlTemplate的ListItem定义它自己的触发器,这似乎采取precendence在由风格定义的触发器(也许有人可以证实这一点,并指出我的一些资源,其中这种行为被定义).
我的解决方案是同时ControlTemplate为我定义一个ListItem:
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true" Background="LightBlue" Margin="0">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
Alm*_*und 14
对Aero风格的一点反思为我们解释了为什么这个简单的触发设置不起作用.
ListBoxItem有一个ControlTemplate,其触发器优先于我们的触发器.对于MultiTrigger来说,至少这似乎是正确的.我设法覆盖了Selected = true的简单触发器,但是对于多重触发器,我必须创建自己的ControlTemplate.
这是Aero风格的模板,显示有问题的MultiTrigger:
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Bd" Value="{DynamicResource {x:Static HighlightBrush}}" Property="Background" />
<Setter Value="{DynamicResource {x:Static HighlightTextBrush}}" Property="Foreground" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd" Value="{DynamicResource {x:Static ControlBrush}}" Property="Background" />
<Setter Value="{DynamicResource {x:Static ControlTextBrush}}" Property="Foreground" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Value="{DynamicResource {x:Static GrayTextBrush}}" Property="Foreground" />
</Trigger>
</ControlTemplate.Triggers>
<Border Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
希望它能稍微清除一些东西.我无法理解为什么他们这么复杂的风格.
删除IsSelected触发器并添加到列表框:
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Red" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Red" />
</ListBox.Resources>
Run Code Online (Sandbox Code Playgroud)
第一次刷第二次,否则