Change Background Color of SelectedItem in ListBox in .NET 4.5 WPF

霸气de小男生 提交于 2019-12-07 14:15:00

问题


I want to change the background color of a ListBox's SelectedItem. I tried the below code :

<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>

But I cannot see any change in SelectedItems's Background Color. Can anybody point the mistake in above XAML?

Also I want to use this style for this specific ListBox, so I don't want to change ControlTemplate.


回答1:


In .NET 4.5 system does not use SystemColors by default, therefore you should:

1) create your own Style/ControlTemplate;

2) create a BlankListBoxContainer like in this example:

<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) remove the difference between the frameworks, like this:

FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false;

before any Window are created, for example before InitializeComponent().

From MSDN:

AreInactiveSelectionHighlightBrushKeysSupported:

Gets or sets a value that indicates whether the application should use the InactiveSelectionHighlightBrush and InactiveSelectionHighlightTextBrush properties for the colors of inactive selected items.




回答2:


Try this:

                <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>


来源:https://stackoverflow.com/questions/22132581/change-background-color-of-selecteditem-in-listbox-in-net-4-5-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!