wpf - 列表框选定文本颜色

标签 wpf visual-studio visual-studio-2010 xaml

今天的第二个问题,但希望很简单,我只需要更改列表框控件中所选项目的前景色。

我有以下样式(它会更改选择时的背景但不会更改前景色):

    <Style TargetType="ListBoxItem" x:Key="PinnedListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF9CC164"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF9CC164"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
    </Style.Resources>
</Style>

这也行不通

    <Style TargetType="ListBoxItem" x:Key="PinnedListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF9CC164"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF9CC164"/>
    </Style.Resources>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

我有以下列表框:

                <ListBox Style="{StaticResource PinnedList}" Height="Auto" MaxHeight="200" Visibility="Hidden" HorizontalAlignment="Left" Margin="-8,45,0,0" SelectionChanged="ui_lsbPinnedORGs_SelectionChanged"
                     SelectedItem="{Binding Path=SelectedPinnedORGsViewModel, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                     SelectionMode="Single" ItemsSource="{Binding Path=ORGViewModels, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" MouseEnter="ui_lsbPinnedORGs_MouseEnter"
                     VerticalAlignment="Top" Width="158" Name="ui_lsbPinnedORGs" ItemContainerStyle="{StaticResource PinnedListBoxItem}">
                <ListBox.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <Label Content="{Binding Path=ORG.Acronym, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" />
                    </HierarchicalDataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

最佳答案

你应该使用像这样的基本触发器:

<Style TargetType="ListBoxItem">
  ...
  <Style.Triggers>
     <Trigger Property="IsSelected" Value="True">
        <Setter Property="Foreground" Value="Color"/>
     </Trigger>
  </Style.Triggers>
</Style>

更新

使用当前模板:

<ListBox.ItemTemplate>
   <HierarchicalDataTemplate>
      <Label Content="{Binding}" Foreground="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=Foreground}" />
   </HierarchicalDataTemplate>
</ListBox.ItemTemplate>

关于wpf - 列表框选定文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9878389/

相关文章:

c++ - Class 对象上的多线程

c# - 将数字转换为字符

asp.net - 什么技术(如果有的话)将取代 ASP.NET?

wpf - 如何在WPF中运行时获取控件的XY坐标?

c++ - XML Lite 解析问题 - 解析时忽略无效数据

visual-studio - 使用 VS 将 C++ 库编译为静态库和动态 dll

wpf - PowerShell/WPF : Disable Button on Click

c - 在 WPF tabControl 中,如何从对象类型中动态选择标签页?

C# 问题 - 如何使用 try 中代码的特定可能异常填充 catch 语句?

visual-studio-2010 - C++ Visual Studio中的 “C3145”和 “C2061”错误