2012-03-26 75 views
2

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

我有以下样式(更改上选择背景,但不是前景色):

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

你想改就click事件或者只是永久地改变它? – 2012-03-26 19:27:42

+0

点击一下,然后一旦未选中就变​​回默认(黑色)。 – 2012-03-26 19:31:42

回答

14

您应该使用基本触发条件:

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

UPDATE

随着当前模板:

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

完全不起作用 - 是我尝试的第一件事。 – 2012-03-26 19:42:49

+0

也许与ListBox的实现方式有关? – 2012-03-26 19:48:51

+1

这是因为您正在使用数据模板。 ListBoxItem的前景确实发生了变化。分层模板中的标签的前景没有。 – 2012-03-26 19:50:33

0

这个安装程序做你想做的吗?

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

没有默认的Setter属性不适用于ListBox - 这就是为什么我重写SystemColors类的背景选择。 两种情况下都经过测试,无论选择何种方式都不起作用。 – 2012-03-26 19:36:50