将控件的Datacontext设置为属性

时间:2013-02-28 13:32:44

标签: wpf binding datacontext

我正在尝试为文本块创建样式以创建高光效果。此样式将用于许多TextBlock,每个TextBlock都绑定到不同的属性。我的主控件的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>

但我需要更改绑定,以便可以在不同的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}"/>

当我尝试这个时,更改属性不会突出显示文本块。有没有办法实现这个目标?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

最终的问题是由于我的文本块位于自定义控件中的内容呈现器内部。自定义控件缺少代码以将其内容演示者添加为逻辑子节点,以便绑定可以正常工作。有关如何执行此操作的信息,请参阅本文的底部部分。 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/feef9913-9371-4377-a621-c14aa998cc6e/