2011-02-02 19 views
2

にバインドされたObservableCollection<Object1>タイプ(Messages以下のコード)があります。 Object1は、2つのプロパティ、すなわちErrMsgIsErrorを持っています。 ErrMsgを赤色で表示するには、エラー(つまり、がtrueの場合)、そうでない場合は黒色にします。TextblockスタイルdataTriggerがItemControl内で機能しない

<ItemsControl 
    Height="Auto" 
    Background="White" 
    ItemsSource="{Binding Messages}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock 
        Margin="5,0,0,0" 
        Text="{Binding ErrMsg}" 
        Width="Auto" 
        Foreground="Black"> 
        <TextBlock.Style> 
         <Style TargetType="{x:Type TextBlock}">  
          <Style.Triggers>   
           <DataTrigger 
            Binding="{Binding IsError}" 
            Value="true">  
            <Setter 
             Property="TextBlock.Foreground" 
             Value="Red" />   
           </DataTrigger>  
          </Style.Triggers>  
         </Style> 
        </TextBlock.Style> 
       </TextBlock> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

問題は、すべてのメッセージが常にIsError値に関係なく、ブラック色で表示されているということでしょうか?

どうすればこの問題を解決できますか?

答えて

7

テキストブロック宣言にForeground="Black"と指定したからです。ローカル値(要素自体に設定)は、スタイル値(トリガーを含む)をオーバーライドします。この問題を解決するには

、単にスタイルに黒の前景の設定を移動:

<TextBlock Margin="5,0,0,0" 
      Text="{Binding Value}" 
      Width="Auto"> 
    <TextBlock.Style> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="Foreground" 
        Value="Black"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsError}" 
          Value="true"> 
        <Setter Property="Foreground" 
          Value="Red" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </TextBlock.Style> 
</TextBlock> 
0

私はあなたは自分の財産からTextBlockプレフィックスを削除し、スタイルに黒にフォアグラウンドを設定する必要があると思う:

<Style TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground" Value="Black"/> 
    <Style.Triggers>   
     <DataTrigger Binding="{Binding IsError}" Value="true">  
      <Setter Property="Foreground" Value="Red" />   
     </DataTrigger>  
    </Style.Triggers> 
    </Style> 

あなたは、通常タイプのみでプロパティを修飾する必要があります(であるべきですかっこ内)、またはストーリーボード用のパスの場合に使用します。

関連する問題