1

我正在尝试为文本块创建一种样式以创建高亮效果。此样式将用于多个 TextBlock,每个都绑定到不同的属性。我的主控件的数据上下文在后面的代码中设置:

this.DataContext = dataobject;

我可以使用这样的工作绑定创建一个文本块:

<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>

但我需要更改绑定,以便可以在不同的 TextBlocks 上使用该样式。就像是:

<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 回答