WPF绑定到可视树中某处的附加属性

时间:2014-07-26 23:50:25

标签: wpf dependency-properties

我有附加财产ZoneBackground。这可以在任何框架元素上注明。

我现在有一个样式ZonedTextBox。这应该将ZoneBackground的值应用于文本框。线索是:样式不知道在视觉层次ZoneBackground中注明的位置以及哪个元素。

是否可以搜索具有ZoneBackground值的第一个父级并使用该值?

我得到了这个XAML:

<Grid controls:ZoneStylingBehavior.ZoneBackground="Red">
    ...
    <TextBox Background="{Binding controls:ZoneStylingBehavior.ZoneBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" />
    ...
</Grid>

这不起作用,我还希望能够在ZoneBackgroundStackPanel之类的可见树中的任何位置记录Grid,并且可能多次。


更新:正如建议我尝试使用dp继承。这个想法听起来不错,但我还没有成功。

DP声明:

public static readonly DependencyProperty ZoneBackgroundProperty = DependencyProperty.RegisterAttached("ZoneBackground", typeof(Brush), typeof(ZoneStylingBehavior), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

XAML:

<StackPanel>
    <Grid controls:ZoneStylingBehavior.ZoneBackground="{StaticResource BrushGreen}">
         ...
         <Label Background="{Binding controls:ZoneStylingBehavior.ZoneBackground, RelativeSource={RelativeSource Self}, PresentationTraceSources.TraceLevel=High}" Content="test" />
         ...
    </Grid>
    ...
    <Grid controls:ZoneStylingBehavior.ZoneBackground="{StaticResource BrushRed}">
        ...
    </Grid>
</StackPanel>

控制台输出:

System.Windows.Data Error: 40 : BindingExpression path error: 'controls:ZoneStylingBehavior' property not found on 'object' ''TextBox' (Name='')'. BindingExpression:Path=controls:ZoneStylingBehavior.ZoneBackground; DataItem='TextBox' (Name=''); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

级别High上的绑定跟踪显示:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=17086942) for Binding (hash=33055417)
System.Windows.Data Warning: 58 :   Path: 'controls:ZoneStylingBehavior.ZoneBackground'
System.Windows.Data Warning: 60 : BindingExpression (hash=17086942): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=17086942): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=17086942): Attach to System.Windows.Controls.Label.Background (hash=18524697)
System.Windows.Data Warning: 67 : BindingExpression (hash=17086942): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=17086942): Found data context element: <null> (OK)
System.Windows.Data Warning: 72 :   RelativeSource.Self found Label (hash=18524697)
System.Windows.Data Warning: 78 : BindingExpression (hash=17086942): Activate with root item Label (hash=18524697)
System.Windows.Data Warning: 108 : BindingExpression (hash=17086942):   At level 0 - for Label.controls:ZoneStylingBehavior found accessor <null>
System.Windows.Data Error: 40 : BindingExpression path error: 'controls:ZoneStylingBehavior' property not found on 'object' ''Label' (Name='')'. BindingExpression:Path=controls:ZoneStylingBehavior.ZoneBackground; DataItem='Label' (Name=''); target element is 'Label' (Name=''); target property is 'Background' (type 'Brush')
System.Windows.Data Warning: 103 : BindingExpression (hash=17086942): Replace item at level 1 with {NullDataItem}
System.Windows.Data Warning: 80 : BindingExpression (hash=17086942): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 88 : BindingExpression (hash=17086942): TransferValue - using fallback/default value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=17086942): TransferValue - using final value <null>

有人可以指出什么是错的吗?

1 个答案:

答案 0 :(得分:4)

不是在Visual树中搜索设置此值的第一个父级,而是通过设置 inherit by default 标志将附加属性设为 FrameworkPropertyMetadataOptions.Inherits 在注册时附加财产。

样品:

public static readonly DependencyProperty ZoneBackgroundProperty =
         DependencyProperty.RegisterAttached("ZoneBackground",
                                      typeof(Brush), typeof(ZoneStylingBehavior),
       new FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.Inherits));

因此,这样它将自动从Visual Tree中的第一个父级继承值,并为附加属性设置值。 (DataContext DP也可以这样工作)

<强> 更新

对于附加属性绑定,您需要在括号中包装绑定。

<Label Background="{Binding (controls:ZoneStylingBehavior.ZoneBackground), 
                            RelativeSource={RelativeSource Self}}"
       Content="test"/>