使用 C# 更改 WPF 列表框 SelectedItem 文本颜色和 highlight/background 颜色

Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

我正在尝试在运行时更改 wpf 列表框的突出显示(选定)颜色和突出显示的文本颜色。我试过创建样式并按如下方式应用它:

    Style s = new Style(typeof(ListBox));
    s.Resources.Add(SystemColors.HighlightBrushKey, Setting.ListSelectedColor);
    s.Resources.Add(SystemColors.HighlightTextBrushKey, Setting.ListSelectedTextColor);
    lstGames.Style = s;

但这似乎没有任何作用。有什么办法可以实现吗?

编辑:

根据建议,我尝试使用 DynamicResources 来实现此目的,但到目前为止也没有成功。我的代码:

动态资源

<UserControl.Resources>
    <Color x:Key="ListTextSelectedColor"/>
    <Color x:Key="ListSelectedColor"/>
</UserControl.Resources>

列表框

        <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" 
             Name="lstGames" Margin="20" Grid.Row="2" Grid.Column="2" 
             SelectionChanged="lstGames_SelectionChanged" Grid.RowSpan="2" Grid.ColumnSpan="2" 
             Background="{x:Null}" BorderBrush="{x:Null}" SelectionMode="Single"
             FontSize="18" FontFamily="OCR A Extended">
        <Style TargetType="ListBox">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource ListSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource ListSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/>
            </Style.Resources>
        </Style>
    </ListBox>

在 C# 中应用资源

this.Resources["ListSelectedColor"] = SETING.ListSelectedColor.Color;
this.Resources["ListTextSelectedColor"] = SETTING.ListSelectedTextColor.Color;

解法:

<Window x:Class="ListBoxStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:ListBoxStyle"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border"
                                Padding="2"
                                SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
                 Width="200" Height="250"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 ScrollViewer.HorizontalScrollBarVisibility="Auto">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>Hi</ListBoxItem>
        </ListBox>
    </Grid>
</Window>

多亏了 Vinkal 和 failedprogramming,我让一切都运行得很好。我创建了以下资源:

<UserControl.Resources>
        <SolidColorBrush x:Key="ListTextSelectedColor" x:Shared="False"/>
        <SolidColorBrush x:Key="ListSelectedColor" x:Shared="False"/>
        <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border"
                                Padding="2"
                                SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="{DynamicResource ResourceKey=ListSelectedColor}"/>
                                <Setter Property="Foreground" Value="{DynamicResource ResourceKey=ListTextSelectedColor}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

然后将样式应用到我的列表框:

ItemContainerStyle="{DynamicResource ResourceKey=_ListBoxItemStyle}"

最后,通过执行以下操作在我的 C# 代码中更改 solidcolorbrush 资源(因此更改 setter 值):

    this.Resources["ListSelectedColor"] = EmulatorPage.ListSelectedColor;
    this.Resources["ListTextSelectedColor"] = EmulatorPage.ListSelectedTextColor;

谢谢你们!

致所有的邻居……不要失去希望! 可以做到!

我从 VSS 开始,右键单击列表框并使用每个 "Edit Template" 和 "Edit Additional Templates" 用于每个可用的东西,直到我发现这些东西是如何工作的。

您可以从一个列表框开始,像往常一样绑定到 MVVM。

<ListBox Width="100"
    x:Name="myComboBox" Margin="8"
    ItemsSource="{Binding ListBoxListSource}"
    SelectedIndex="{Binding ListBox}">
</ListBox>

在 UserControl 或 Window Resources 中设置一些东西....

ListBoxStyle - 这是列表框主容器的样式,您可以在此处设置主框的边框、边距、填充等。对于我的示例,我只是摆脱了一切以取消样式。

<UserControl.Resources>
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Margin" Value="0"/>
    </Style>
</UserControl.Resources>

ItemContainerStyle - 这是人们所说的无法重新设置样式的部分 - 它在选择项目时包含 "windows-selector-blue" 栏,但不要害怕这也可以重新设置样式(将 UserControl.Resources 部分与上面的部分合并)。

本部分 > 将 ItemContainer 的模板从任何形式更改为 Border,将上边距设置为 3 以填充内容并设置样式。我们对这种样式所做的就是在项目的左侧和右侧添加 3px 透明边框。然后在Triggers>IsSelected (target of myBorder)中,将border Brush改为Red.

<UserControl.Resources>
    <Style x:Key="ItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="myBorder"
                                    Padding="0" Margin="0 3 0 0"
                                    SnapsToDevicePixels="true"
                                     Style="{DynamicResource borderContent}">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Resources>
                        <Style x:Key="borderContent" TargetType="Border">
                            <Setter Property="BorderThickness" Value="3 0 3 0"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                        </Style>
                    </ControlTemplate.Resources>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="myBorder" Property="BorderBrush" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

ListBoxItemDataTemplate - 下一步是制作显示数据的项目容器。在我的示例中,YourTextBlockStyler 在 Text>binding 上有一个触发器,并更改文本的前景色和背景色。请注意,Listbox 样式的前景和背景设置为透明,因此如果您想看到任何东西,您必须在 TextBlock 样式中覆盖它们。

<UserControl.Resources>
    <DataTemplate x:Key="ListBoxItemDataTemplate">
        <TextBlock Text="{Binding}" Style="{StaticResource YourTextBlockStyler}"/>
    </DataTemplate>
</UserControl.Resources>

返回列表框 - 现在我们已经在资源部分设置了所有样式和模板,我们可以使用 Style="" ItemContainerStyle="" 更新列表框, ItemTemplate=""

<ListBox Width="100"
    x:Name="myComboBox" Margin="8"
    ItemsSource="{Binding ListBoxListSource}"
    SelectedIndex="{Binding ListBox}"
    Style="{StaticResource ListBoxStyle}"
    ItemContainerStyle="{StaticResource ItemContainerStyle}"
    ItemTemplate="{StaticResource ListBoxItemDataTemplate}">
</ListBox>

然后您无聊的列表框将神奇地变成带有红色边框选择器的完全重新设计的列表框

来自 into

全部不编辑一个System.ResourceBrush =]