【问题标题】:Set Datacontext of a control to a property将控件的 Datacontext 设置为属性
【发布时间】:2013-02-28 13:32:43
【问题描述】:

我正在尝试为文本块创建样式以创建高亮效果。此样式将用于多个 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}"/>

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

【问题讨论】:

  • 我可能遗漏了一些东西,但您的代码的底部运行良好。如果我设置 BooleanProperty1 = true 我看到一个黄色背景,否则它是白色的......(对于另一个 TextBlock 中的 BooleanProperty2 相同)也许你错过了 INPC?
  • 你是如何设置布尔属性的?它们是 dataObject 的一部分并且确实实现了 INPC。当我在运行时查看代码时,属性为 true,但控件的 DataContext 显示为 false。
  • 原来问题是文本块在内容呈现器内部,这意味着当我尝试将数据上下文设置得更低时,绑定要么不起作用,要么保持静态。这些 cmets 帮助回答了我的问题,但我不知道如何将其标记为答案。
  • 最终问题是由于我的文本块位于自定义控件中的内容呈现器内部。自定义控件缺少将其内容呈现器添加为逻辑子级的代码,以便绑定能够正常工作。有关如何执行此操作的信息,请参阅本文的底部。 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/feef9913-9371-4377-a621-c14aa998cc6e/

标签: wpf binding datacontext


【解决方案1】:

总是会滥用标签属性。

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

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2013-09-23
    • 1970-01-01
    • 2011-03-23
    • 2011-03-12
    相关资源
    最近更新 更多