ListBox选定的文本颜色

时间:2012-03-26 19:24:29

标签: 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:

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

2 个答案:

答案 0 :(得分:15)

你应该使用基本的触发器,如:

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

答案 1 :(得分:0)

这个Setter能做你想做的吗?

    <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>
        <Setter Property="Foreground" Value="Aqua"/>
    </Style>