Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF绑定到可视化树中某个位置的附加属性_Wpf_Dependency Properties - Fatal编程技术网

WPF绑定到可视化树中某个位置的附加属性

WPF绑定到可视化树中某个位置的附加属性,wpf,dependency-properties,Wpf,Dependency Properties,我有一个附加属性ZoneBackground。这可以在任何框架元素上注意到 我现在有了一个样式zoneTextBox。这应将ZoneBackground的值应用于文本框。线索是:这种风格不知道视觉层次结构中的什么地方以及在哪个元素上标注了ZoneBackground 是否可以搜索具有ZoneBackground值的第一个父级并使用该值 我得到了这个XAML: <Grid controls:ZoneStylingBehavior.ZoneBackground="Red"> ..

我有一个附加属性
ZoneBackground
。这可以在任何框架元素上注意到

我现在有了一个样式
zoneTextBox
。这应将
ZoneBackground
的值应用于文本框。线索是:这种风格不知道视觉层次结构中的什么地方以及在哪个元素上标注了
ZoneBackground

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

我得到了这个XAML:

<Grid controls:ZoneStylingBehavior.ZoneBackground="Red">
    ...
    <TextBox Background="{Binding controls:ZoneStylingBehavior.ZoneBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" />
    ...
</Grid>
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>
级别
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>
System.Windows.Data警告:56:为绑定(哈希=33055417)创建了BindingExpression(哈希=17086942)
System.Windows.Data警告:58:Path:“控件:ZoneStylingBehavior.ZoneBackground”
System.Windows.Data警告:60:BindingExpression(哈希=17086942):默认模式解析为单向
System.Windows.Data警告:61:BindingExpression(哈希=17086942):默认更新触发器已解析为PropertyChanged
System.Windows.Data警告:62:BindingExpression(哈希值=17086942):附加到System.Windows.Controls.Label.Background(哈希值=18524697)
System.Windows.Data警告:67:BindingExpression(哈希=17086942):解析源
System.Windows.Data警告:70:BindingExpression(哈希=17086942):找到数据上下文元素:(确定)
System.Windows.Data警告:72:RelativeSource.Self-found标签(哈希值=18524697)
System.Windows.Data警告:78:BindingExpression(哈希值=17086942):使用根项标签激活(哈希值=18524697)
System.Windows.Data警告:108:BindingExpression(哈希=17086942):级别为0-对于Label.controls:ZoneStylingBehavior已找到访问器
System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“标签”(名称=“”)上找不到“控件:ZoneStylingBehavior”属性。BindingExpression:Path=controls:ZoneStylingBehavior.ZoneBackground;DataItem='Label'(名称='');目标元素为“标签”(名称=“”);目标属性为“背景”(类型为“笔刷”)
System.Windows.Data警告:103:BindingExpression(哈希=17086942):将级别1的项替换为{NullDataItem}
System.Windows.Data警告:80:BindingExpression(哈希=17086942):TransferValue-获取原始值{DependencyProperty.UnsetValue}
System.Windows.Data警告:88:BindingExpression(哈希=17086942):TransferValue-使用回退/默认值
System.Windows.Data警告:89:BindingExpression(哈希=17086942):TransferValue-使用最终值

有人能指出问题出在哪里吗?

不要在设置了此值的可视化树中搜索第一个父级,而是通过设置

样本:

public static readonly DependencyProperty ZoneBackgroundProperty =
         DependencyProperty.RegisterAttached("ZoneBackground",
                                      typeof(Brush), typeof(ZoneStylingBehavior),
       new FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.Inherits));
所以,通过这种方式,它将自动从可视树中的第一个父级继承值,并为附加属性设置值。(DataContext DP也可以这样工作)

更新

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

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


那么,创建ZoneBackground和继承的财产怎么样,那么您只需要做这样的事情看起来是个好主意:不幸的是,我无法让它工作。也许我遗漏了一个小细节。我更新了这个问题。@ZoolWay-请参阅回答中的更新,了解您的后续问题。哦,我没有注意到!谢谢你也给我指出了这个语法问题!:)
public static readonly DependencyProperty ZoneBackgroundProperty =
         DependencyProperty.RegisterAttached("ZoneBackground",
                                      typeof(Brush), typeof(ZoneStylingBehavior),
       new FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.Inherits));
<Label Background="{Binding (controls:ZoneStylingBehavior.ZoneBackground), 
                            RelativeSource={RelativeSource Self}}"
       Content="test"/>