2012-03-26 37 views
2

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"/> 
    </Style.Resources> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Foreground" Value="White"/> 
     </Trigger> 
    </Style.Triggers> 
</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"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> 
    </Style.Resources> 
</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

:現在のテンプレートに

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

UPDATE

それとも永久に変更するのですか? –

+0

クリックして、一度選択しないとデフォルト(黒)に戻ります。 –

答えて

14

:あなたがクリックイベントでそれを変更したいん

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

全く動作しません - 私が試した最初のことでした。 –

+0

おそらくListBoxの実装方法と関係がありますか? –

+1

これは、あなたがdatatemplateを使用しているからです。 ListBoxItemのフォアグラウンドは実際に変更されています。階層テンプレート内のラベルのフォアグラウンドはそうではありません。 –

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クラスをオーバーライドする理由です。 いずれの場合も、選択に関係なくテストされ、動作しません。 –

関連する問題