将控件的数据上下文设置为属性

问题描述 投票:0回答:1

我正在尝试为文本块创建样式以创建突出显示效果。此样式将用于许多TextBlocks,每个TextBlocks绑定到不同的属性。我的主控件的datacontext在后面的代码中设置:

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}"/>

当我尝试这样做时,更改属性不会突出显示文本块。有没有办法做到这一点?

wpf binding datacontext
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>
© www.soinside.com 2019 - 2024. All rights reserved.