更改列表框WPF文字的SelectedItem颜色和高亮区/背景颜色使用C# [英] Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

查看:275
本文介绍了更改列表框WPF文字的SelectedItem颜色和高亮区/背景颜色使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改高亮显示(选择)的颜色,并在运行一个WPF列表框的高亮文本颜色。我曾尝试创建一个样式和应用,如下所示:

 样式S =新样式(typeof运算(列表框)); 
s.Resources.Add(SystemColors.HighlightBrushKey,Setting.ListSelectedColor);
s.Resources.Add(SystemColors.HighlightTextBrushKey,Setting.ListSelectedTextColor);
lstGames.Style = S;



但这似乎什么也不做。有什么办法来实现这一目标。



编辑:



每建议,我尝试使用DynamicResources来实现这一但到目前为止,这还没有成功无论是。 UserControl.Resources>



DYNAMICRESOURCES

 <:我对这段代码; 
<颜色X:键=ListTextSelectedColor/>
<颜色X:键=ListSelectedColor/>
< /UserControl.Resources>



LISTBOX

 <列表框ScrollViewer.Horizo​​ntalScrollBarVisibility =隐藏ScrollViewer.VerticalScrollBarVisibility =隐藏
NAME =lstGames保证金=20Grid.Row =2Grid.Column =2
的SelectionChanged =lstGames_SelectionChangedGrid.RowSpan =2Grid.ColumnSpan =2
背景={X:空}BorderBrush ={X:空}的SelectionMode =单
字号=18的FontFamily =OCR一个扩展>
<风格的TargetType =列表框>
< Style.Resources>
<的SolidColorBrush X:键={X:静态SystemColors.HighlightBrushKey}颜色={DynamicResource ListSelectedColor}/>
<的SolidColorBrush X:键={X:静态SystemColors.ControlBrushKey}颜色={DynamicResource ListSelectedColor}/>
<的SolidColorBrush X:键={X:静态SystemColors.HighlightTextBrushKey}颜色={DynamicResource ListTextSelectedColor}/>
<的SolidColorBrush X:键={X:静态SystemColors.ControlTextBrushKey}颜色={DynamicResource ListTextSelectedColor}/>
< /Style.Resources>
< /样式和GT;
< /列表框>



投入更多资源在C#



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


解决方案

解决方案:

 <窗口x:类=ListBoxStyle.MainWindow
的xmlns =http://schemas.microsoft.com / WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:SRC =CLR的命名空间:ListBoxStyle
标题=主窗口HEIGHT =350WIDTH =525>
< Window.Resources>
<风格X:键=_ ListBoxItemStyle的TargetType =ListBoxItem的>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType =ListBoxItem的>
< BORDER名称=_边框
填充=2
SnapsToDevicePixels =真正的>
< ContentPresenter />
< /边框>
< ControlTemplate.Triggers>
<触发属性=IsSelectedVALUE =真正的>
<二传手的TargetName =_边框属性=背景值=黄色/>
< setter属性=前景VALUE =红/>
< /触发>
< /ControlTemplate.Triggers>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /Window.Resources>
<网格和GT;
<列表框ItemContainerStyle ={DynamicResource _ListBoxItemStyle}
WIDTH =200HEIGHT =250
ScrollViewer.VerticalScrollBarVisibility =自动
ScrollViewer.Horizo​​ntalScrollBarVisibility =自动>
<&ListBoxItem的GT;您好< / ListBoxItem的>
<&ListBoxItem的GT;喜< / ListBoxItem的>
< /列表框>
< /网格和GT;
< /窗GT;


I am trying to change the highlighted(selected) color and the highlighted text color of a wpf listbox at runtime. I have tried creating a style and applying it as follows:

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

But this seems to do nothing. Is there any way to achieve this?

EDIT:

Per suggestions, I tried using DynamicResources to achieve this, but so far this has not been successful either. My code for this:

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>

APPLYING RESOURCES IN C#

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

解决方案

Solution:

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

这篇关于更改列表框WPF文字的SelectedItem颜色和高亮区/背景颜色使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆