1. <i id='pUyRc'><tr id='pUyRc'><dt id='pUyRc'><q id='pUyRc'><span id='pUyRc'><b id='pUyRc'><form id='pUyRc'><ins id='pUyRc'></ins><ul id='pUyRc'></ul><sub id='pUyRc'></sub></form><legend id='pUyRc'></legend><bdo id='pUyRc'><pre id='pUyRc'><center id='pUyRc'></center></pre></bdo></b><th id='pUyRc'></th></span></q></dt></tr></i><div id='pUyRc'><tfoot id='pUyRc'></tfoot><dl id='pUyRc'><fieldset id='pUyRc'></fieldset></dl></div>
        <bdo id='pUyRc'></bdo><ul id='pUyRc'></ul>
    2. <legend id='pUyRc'><style id='pUyRc'><dir id='pUyRc'><q id='pUyRc'></q></dir></style></legend>
    3. <small id='pUyRc'></small><noframes id='pUyRc'>

      <tfoot id='pUyRc'></tfoot>

        使用 C# 更改 WPF 列表框 SelectedItem 文本颜色和突出显示/背景颜色

        时间:2023-10-08

          <tbody id='HdmVS'></tbody>
        • <i id='HdmVS'><tr id='HdmVS'><dt id='HdmVS'><q id='HdmVS'><span id='HdmVS'><b id='HdmVS'><form id='HdmVS'><ins id='HdmVS'></ins><ul id='HdmVS'></ul><sub id='HdmVS'></sub></form><legend id='HdmVS'></legend><bdo id='HdmVS'><pre id='HdmVS'><center id='HdmVS'></center></pre></bdo></b><th id='HdmVS'></th></span></q></dt></tr></i><div id='HdmVS'><tfoot id='HdmVS'></tfoot><dl id='HdmVS'><fieldset id='HdmVS'></fieldset></dl></div>

          <small id='HdmVS'></small><noframes id='HdmVS'>

          • <bdo id='HdmVS'></bdo><ul id='HdmVS'></ul>

              <legend id='HdmVS'><style id='HdmVS'><dir id='HdmVS'><q id='HdmVS'></q></dir></style></legend>
                <tfoot id='HdmVS'></tfoot>

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

                  问题描述

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

                  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?

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

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

                  动态资源

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

                  这篇关于使用 C# 更改 WPF 列表框 SelectedItem 文本颜色和突出显示/背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C# WinForms 中为 Listbox/ListView 行的部分着色的方法? 下一篇:MoveFocus 从一个列表框到另一个

                  相关文章

                1. <small id='awI4G'></small><noframes id='awI4G'>

                  <legend id='awI4G'><style id='awI4G'><dir id='awI4G'><q id='awI4G'></q></dir></style></legend>
                  <tfoot id='awI4G'></tfoot>
                2. <i id='awI4G'><tr id='awI4G'><dt id='awI4G'><q id='awI4G'><span id='awI4G'><b id='awI4G'><form id='awI4G'><ins id='awI4G'></ins><ul id='awI4G'></ul><sub id='awI4G'></sub></form><legend id='awI4G'></legend><bdo id='awI4G'><pre id='awI4G'><center id='awI4G'></center></pre></bdo></b><th id='awI4G'></th></span></q></dt></tr></i><div id='awI4G'><tfoot id='awI4G'></tfoot><dl id='awI4G'><fieldset id='awI4G'></fieldset></dl></div>

                    • <bdo id='awI4G'></bdo><ul id='awI4G'></ul>