C# 设置选定ListBoxItem的样式

C# 设置选定ListBoxItem的样式,c#,wpf,xaml,C#,Wpf,Xaml,编辑: <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/2

编辑:

<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-->
<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
    时,该触发器用
    {DynamicSource{x:Static SystemColor.HighlightBrushKey}}
    您可以对
    DataTemplate
    的子项执行相同的操作,也可以替换
    模板
    ,但只需覆盖资源就更容易了

    您也可以全局覆盖相同的资源,以获得一致的外观

    <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>
    
    
    
    我不确定问题出在哪里,但我已经复制并粘贴了该代码行,并且我的
    列表框
    行仍然没有改变颜色。@CBreeze我必须查看您的版本,包括影响
    列表框
    列表框项目
    的所有样式。这可能很简单。例如,如果您的
    BlueItemStyle
    设置了
    ListBoxItem
    Template
    属性,则该属性将被破坏,因为负责使用这些选择笔刷的触发器位于
    ListBoxItem
    的默认控件模板中。请参阅我的编辑。当您提到
    BlueItemStyle
    时,我以为我们已经有了它,但即使完全删除了它,我仍然看不到行高亮显示的更改!我把它改成了红色,以确保我选择的蓝色与原来的不太接近。。我还确保在我的
    ResourceDictionary
    中没有针对
    ListBox
    ListBoxItem
    的其他样式。我还尝试了一个全新的项目,使用了一个非常简单的
    ListBox
    ,除了
    SolidColorBrush
    之外,没有其他样式,也没有运气,所以这个解决方案对我来说真的不起作用。我觉得很难相信这一点。这真的是WPF还是其他XAML?
    <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>