1

ハイライト効果を作成するために、テキストブロックのスタイルを作成しようとしています。このスタイルは、それぞれが異なるプロパティにバインドされている多数の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}"/>

これを試してみると、プロパティを変更してもテキストブロックが強調表示されません。これを達成する方法はありますか?

4

1 に答える 1

2

タグ プロパティを常に悪用する可能性があります。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="Tag" Value="True">
                    <Setter Property="Background" Value="Yellow"/>
                    <Setter Property="Foreground" Value="Black"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBlock Text="Field1" Grid.Row="0" Tag="{Binding Field1}"/>
        <TextBlock Text="Field2" Grid.Row="1" Tag="{Binding Field2}"/>        
    </Grid>
</Window>

または、冒険心があれば、添付の依存関係プロパティを作成して、それを渡すことができます。

public class TextBlockExtender : DependencyObject
{
    public static string GetMyDataField(DependencyObject obj)
    {
        return (string)obj.GetValue(MyDataFieldProperty);
    }

    public static void SetMyDataField(DependencyObject obj, string value)
    {
        obj.SetValue(MyDataFieldProperty, value);
    }

    public static readonly DependencyProperty MyDataFieldProperty =
        DependencyProperty.RegisterAttached("MyDataField", typeof(string), typeof(TextBlockExtender), new PropertyMetadata(null));
}

XAML で使用

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="local:TextBlockExtender.MyDataField" Value="True">
                    <Setter Property="Background" Value="Yellow"/>
                    <Setter Property="Foreground" Value="Black"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBlock Text="Field1" Grid.Row="0" local:TextBlockExtender.MyDataField="{Binding Field1}"/>
        <TextBlock Text="Field2" Grid.Row="1" local:TextBlockExtender.MyDataField="{Binding Field2}"/>
    </Grid>
</Window>
于 2013-02-28T13:40:18.957 に答える