作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个附属物ZoneBackground
.这可以在任何框架元素上注明。
我现在有风格ZonedTextBox
.这应该应用 ZoneBackground
的值到一个文本框。线索是:风格不知道在视觉层次中的哪个位置ZoneBackground
注明和在哪个元素上。
是否可以搜索具有 ZoneBackground
值的第一个父级?并使用该值?
我得到了这个 XAML:
<Grid controls:ZoneStylingBehavior.ZoneBackground="Red">
...
<TextBox Background="{Binding controls:ZoneStylingBehavior.ZoneBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" />
...
</Grid>
ZoneBackground
可视树中的任何位置,例如
StackPanel
,
Grid
并且可能多次。
public static readonly DependencyProperty ZoneBackgroundProperty = DependencyProperty.RegisterAttached("ZoneBackground", typeof(Brush), typeof(ZoneStylingBehavior), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
<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
上的绑定(bind)跟踪显示:
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>
最佳答案
不要在设置此值的可视树中搜索第一个父级,而是将附加属性设置为 inherit by default
通过设置 FrameworkPropertyMetadataOptions.Inherits
注册时附加属性的标志。
样本:
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"/>
关于WPF 绑定(bind)到可视树中某处的附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976557/
我正在尝试获得一个按钮,按下该按钮时会改变颜色。当再次按下时,它应该变回原来的颜色。我究竟做错了什么? 我的模板中的按钮: export default { data: {
我是一名优秀的程序员,十分优秀!