1

私はTabControlを持っており、他のタブの中には「エラー」と呼ばれるものがあります。「ErrorsExist」という特定のプロパティがtrueに設定されている場合、ヘッダーの前景を赤にする必要があります。これが私のコードです:

           <TabControl >
            <TabControl.Resources>
                <conv:ErrorsExistToForegroundColorConverter x:Key="ErrorsExistToForegroundColorConverter"/>

                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Foreground="{Binding  Path=ErrorsExist, Converter={StaticResource ErrorsExistToForegroundColorConverter}}" Text="{Binding}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>

            <TabItem x:Name="ErrorsTab" Header="Errors">

これが私のコンバーターです:

    public class ErrorsExistToForegroundColorConverter: IValueConverter
{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
       switch ((bool)value)
       {
          case true:
             return Brushes.Red;
          case false:
             return Brushes.Black;
          default:
             return Binding.DoNothing;
       }
    }

これには2つの問題があります。

まず、これによりすべてのタブヘッダーが赤に設定され、ErrorsTabタブに対してのみ行う必要があります。

第二に、それはうまくいきません。つまり、コンバーターのConvert()メソッドが呼び出されることはありません。これを手伝ってくれませんか。

ありがとう。

4

1 に答える 1

1

変更したい TabItem にのみスタイルを割り当て、この単純なタスクには DataTrigger をより適切に使用します。

<TabItem x:Name="ErrorsTab" Header="Errors">
    <TabItem.Style>
      <Style TargetType="{x:Type TabItem}">
        <Style.Triggers>
          <DataTrigger Binding="{Binding ErrorsExist}" Value="True">
              <Setter Property="Foreground" Value="Red"/>
          </DataTrigger>
         </Style.Triggers>
       </Style>
    </TabItem.Style>
  </TabItem>

編集:

問題は、TabItem ヘッダーが親 TabItem の DataContext を継承しないことです。これをコンバーターで動作させたい場合は、TabHeader DataContext を手動で設定します。

          <TextBlock DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" 
                     Foreground="{Binding ErrorsExist,Converter={StaticResource ErrorsExistToForegroundColorConverter}}" Text="{Binding}"/>
于 2012-04-04T13:07:30.280 に答える