在.NET 4.5 WPF中更改ListBox中所选项目的背景颜色

3
我想改变ListBox的选定项的背景颜色。 我尝试了以下代码:
<ListBox Grid.Column="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
         ItemsSource="{Binding Parents}" DisplayMemberPath="Title"
         Height="35" FontSize="18" BorderThickness="0" Background="#FF2A2A2A" 
         Foreground="White" SelectedIndex="0">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Foreground" Value="DodgerBlue" />
                </Trigger>
            </Style.Triggers>

            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                                 Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                                 Color="Transparent" />
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

但是我无法看到SelectedItems的背景颜色发生任何变化。有人能指出上述XAML中的错误吗?

此外,我想要将这个样式用于特定的ListBox,所以我不想改变控件模板。


是的,我正在使用.NET 4.5。 - Vishal
2个回答

6

试试这个:

                <ListBox Grid.Column="1" Grid.Row="1" Margin="2" SelectionMode="Multiple" ItemsSource="{Binding NavigationMenuItems}" DisplayMemberPath="Name">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsSelected" Value="true">
                                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                            </Trigger>
                                            <Trigger Property="IsEnabled" Value="false">
                                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>

1
你能否添加一条评论,解释一下你认为原始代码存在的问题以及你为什么认为这个修改可以解决它?这样,你的回答不仅可以帮助提问者,也可以帮助未来遇到类似问题的其他人。 - starsplusplus

5
在.NET 4.5中,默认情况下系统不使用SystemColors,因此您应该:
1)创建自己的样式/控件模板;
2)创建一个BlankListBoxContainer,就像this示例中一样:
<Style x:Key="BlankListBoxContainerStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>

   <Setter Property="FocusVisualStyle" Value="{x:Null} "/>
</Style>

3)移除框架之间的差异,例如this

FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false;

在任何窗口被创建之前,例如在InitializeComponent()之前。

来自MSDN

AreInactiveSelectionHighlightBrushKeysSupported:

获取或设置一个值,指示应用程序是否应使用InactiveSelectionHighlightBrushInactiveSelectionHighlightTextBrush属性作为非活动选定项的颜色。


1
感谢您出色的努力。它解决了问题。 - Vishal

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