的样式为选定的ListBoxItem的
问题描述:
<Window x:Class="test_project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:test_project"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow" Opacity="0.6" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
</ListBox.Resources>
<ListBox.Items>
<ListBoxItem Content="Hello"/>
<ListBoxItem Content="Hello"/>
</ListBox.Items>
</ListBox>
</Grid>
</Window>
我想选择的时候风格ListBoxItem
,使其有一个蓝色背景,白色的文字。我在网上看了很多答案,他们似乎都给出了矛盾的观点,至今都没有为我效力。这里是我的ListBox
:
<ListBox Grid.Row="1" Margin="5" ItemContainerStyle="{StaticResource BlueItemStyle}"
BorderBrush="#06658D"
ItemsSource="{Binding UsersView}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我迄今所使用的样式,并能实现什么:
<!--ListBoxItem-->
<Style TargetType="{x:Type ListBoxItem}" x:Key="BlueItemStyle">
<Setter Property="Height" Value="40"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#06658D"/>
</Trigger>
</Style.Triggers>
</Style>
然而,这并不以任何方式风格ListBoxItem
。简单地说,我的问题是风格选择ListBoxItem
所以ListBox
看起来像这样最简单的方法:
答
该解决方案不能在Windows 10,这意味着它不再适合在所有的,去上班前锋。谢谢,微软。据我所知,这将不得不取代ListBoxItem控件模板。两个问题解决的话:
不能更改所选项目背景颜色的方式;对于选择状态,ListBoxItem
的Background
属性不会更改。相反,ListBoxItem
的默认控制模板使用Background
用于未选中,并且具有触发器,用于在IsSelected
为true
时用某个模板子代替{DynamicResource {x:Static SystemColors.HighlightBrushKey}}
的实际背景刷子。
您可以做同样的你的DataTemplate
的孩子,或者你可以更换Template
但它更容易只是覆盖资源来代替。
为了保持一致的外观,您也可以全局覆盖相同的资源。
<ListBox
Grid.Row="1"
Margin="5"
ItemContainerStyle="{StaticResource BlueItemStyle}"
BorderBrush="#06658D"
ItemsSource="{Binding UsersView}"
>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#06658D" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />
<!--
The default inactive selection color in Win7 is much too pale for my taste;
our older users are unable to distinguish it from white on most monitors.
-->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#06658D" Opacity="0.6" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我不知道问题是什么,但我已经复制并粘贴代码行线和我的'ListBox'行依然不改色.. – CBreeze
@CBreeze我得看看你的版本,包括影响'ListBox'或'ListBoxItem'的所有样式。这可能很简单。例如,如果您的'BlueItemStyle'设置了'ListBoxItem'的'Template'属性,那么这会破坏它,因为负责使用这些选择画笔的触发器在'ListBoxItem'的默认控制模板中。 –
埃德请看我的编辑。当我提到'BlueItemStyle'时,我想我们已经有了它,但即使在完全删除之后,我仍然看不到行高亮度改变!我已经将它改为红色,以确保我选择的蓝色不会太接近原来的颜色。我还确保在我的“ResourceDictionary”中没有其他针对'ListBox'或'ListBoxItem'的样式。 – CBreeze