3

私はスタイルのXAML階層を操作して理解しようとしています...シンプルでシンプルなテキストボックス...「IsEnabled」フラグに基づいて「無効」の背景色を設定する方法については、いたるところに見られます。よかった。

ここで、TextBox...MyTextBoxから派生した別のクラスが必要です。このクラスには、プロパティがあります(依存関係プロパティではないため、DataTriggerを使用していました)。したがって、機能していた通常のTextBoxアクションをすべて保持したいのですが、新しいトリガーを取得して、背景色を他の色に適切に更新します。これが私が持っているものです。明確にするために、色に関する私の静的リソースはすべてSOLIDBRUSHESです...

<Style TargetType="TextBox" >
  <Setter Property="FontFamily" Value="Courier New" />
  <Setter Property="FontSize" Value="12" />
  <Setter Property="Foreground" Value="{StaticResource MyForeground}" />
  <Setter Property="Background" Value="{StaticResource MyBackground}" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="TextBox">
        <Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" 
            BorderBrush="{TemplateBinding BorderBrush}"
            Background="{TemplateBinding Background}" 
            SnapsToDevicePixels="true">

            <ScrollViewer Name="PART_ContentHost" 
                Background="{TemplateBinding Background}" 
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Border>

        <ControlTemplate.Triggers>
          <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{StaticResource MyDisBackground}" />
            <Setter TargetName="PART_ContentHost" Property="Background" 
                 Value="MyDisBackground"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!--  Now, my derived (or so I was hoping) style that just adds additional trigger -->
<Style TargetType="local:MyTextBox"  BasedOn="{StaticResource {x:Type TextBox}}" >
  <Style.Triggers>
    <DataTrigger Binding="{Binding Path=IsRequired}" Value="True">
      <Setter Property="Background" Value="{StaticResource RequiredBackground}" />
    </DataTrigger>
  </Style.Triggers>
</Style>

私は何か簡単なものが欠けていますか?

4

1 に答える 1

1

まず、あなたはあなたの(コントロール自体DataTriggerではなく)を見ています。したがって、コントロールを見てください。次のようなことを行う必要があります。DataContextMyTextBox

<DataTrigger Binding="{Binding Path=IsRequired, RelativeSource={RelativeSource Self}}" Value="True">
    <Setter Property="Background" Value="{StaticResource RequiredBackground}" />
</DataTrigger>

MyTextBox.Backgroundこれで、trueの場合にプロパティが設定されMyTextBox.IsRequiredます。ただし、依存関係プロパティの値には優先順位があります。したがって、上記は次のように使用される背景を視覚的に変更します。

<local:MyTextBox />

次の場合、RequiredBackgroundブラシは使用されません。MyDisBackground代わりに、ブラシが表示されます。

<local:MyTextBox IsEnabled="False" />

この場合、ScrollViewer.Backgroundはに変更されMyDisBackground、プロパティにバインドされなくなりMyTextBox.Backgroundます。はMyTextBox.Backgroundまだ使用されますがRequiredBackground、どこでも使用されなくなりました。

最後に、次の場合、RequiredBackgroundブラシは使用されません。

<local:MyTextBox Background="Yellow" />

ここで、ローカル値(黄色)は優先順位リストの#3にあり、スタイルセッターは#8にあります。

プロパティをデフォルトでfalseに設定されている依存関係プロパティにすると、次のようになります。

<Style TargetType="TextBox" >
  <Setter Property="FontFamily" Value="Courier New" />
  <Setter Property="FontSize" Value="12" />
  <Setter Property="Foreground" Value="{StaticResource MyForeground}" />
  <Setter Property="Background" Value="{StaticResource MyBackground}" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="TextBox">
        <Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" 
            BorderBrush="{TemplateBinding BorderBrush}"
            Background="{TemplateBinding Background}" 
            SnapsToDevicePixels="true">

            <ScrollViewer Name="PART_ContentHost" 
                Background="{TemplateBinding Background}" 
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Border>

        <ControlTemplate.Triggers>
          <Trigger Property="local:MyTextBox.IsRequired" Value="False">
            <Setter Property="Background" Value="{StaticResource RequiredBackground}" />
            <Setter TargetName="PART_ContentHost" Property="Background" 
                 Value="RequiredBackground"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{StaticResource MyDisBackground}" />
            <Setter TargetName="PART_ContentHost" Property="Background" 
                 Value="MyDisBackground"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<Style TargetType="local:MyTextBox"  BasedOn="{StaticResource {x:Type TextBox}}" />

TextBoxのプロパティは存在しませんが、依存関係プロパティのデフォルト値を取得してトリガーすることができます。ただし、TextBoxに設定されるため、そのトリガーは使用されません。

于 2011-10-19T21:00:57.700 に答える