ハイライト効果を作成するために、テキストブロックのスタイルを作成しようとしています。このスタイルは、それぞれが異なるプロパティにバインドされている多数のTextBlockで使用されます。メインコントロールのデータコンテキストは、コードビハインドで設定されています。
this.DataContext = dataobject;
次のように機能するバインディングを使用して、1つのテキストブロックを作成できます。
<TextBlock Text="Field1">
<TextBlock.Style>
<Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.}" Value="True">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
<TextBlock>
ただし、スタイルをさまざまなTextBlockで使用できるように、バインディングを変更する必要があります。何かのようなもの:
<Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.}" Value="True">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBlock Text="Field1" DataContext="{Binding Path=BooleanProperty1}" Style="{StaticResource HighlightStyle}"/>
<TextBlock Text="Field2" DataContext="{Binding Path=BooleanProperty2}" Style="{StaticResource HighlightStyle}"/>
これを試してみると、プロパティを変更してもテキストブロックが強調表示されません。これを達成する方法はありますか?