使用C#更改WPF Listbox SelectedItem文本颜色和高亮/背景颜色

时间:2015-02-24 01:25:36

标签: c# wpf listbox

我正在尝试在运行时更改突出显示的(选定的)颜色和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来实现这一目标,但到目前为止,这还没有成功。我的代码:

DYNAMICRESOURCES

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

LISTBOX

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

3 个答案:

答案 0 :(得分:21)

<强>解决方案:

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

答案 1 :(得分:8)

感谢Vinkal并且编程失败,我让一切都运转得很漂亮。我创建了以下资源:

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

谢谢你们两位!

答案 2 :(得分:0)

对那些邻居说话的人......不要失去希望! 可以完成

我从VSS开始,右键单击列表框,并为每个可用的东西使用每个“编辑模板”和“编辑其他模板”,直到找到这些东西的工作方式。

你很简单地用一个列表框开始,正常绑定到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部分与上面的部分合并)。

此部分是&gt;将ItemContainer的模板从它的任何内容更改为Border,设置上边距为3以填充内容并设置样式。我们用这种风格做的就是在项目的左右两侧添加3px透明边框。然后在Triggers&gt; IsSelected(myBorder的目标)中,将边框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&gt;绑定上有一个触发器,并更改文本的前景色和背景色。请注意,列表框样式的前景和背景设置为透明,因此如果要查看任何内容,则必须以TextBlock样式覆盖它们。

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

返回列表框 - 现在我们已经在参考资料部分设置了所有样式和模板,我们可以使用Style =“”ItemContainerStyle =“”和ItemTemplate =“”<来更新列表框/ p>

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

然后你的无聊列表框将神奇地转换为带有红色边框选择器的完全重新设计的列表框

Boring standard list boxTotally restyled list box with red border selector

所有没有编辑单个System.ResourceBrush =]