请问为什么WPF绑定失效?

q199308040000 2018-10-09 02:21:19
先用伪代码说明一下发生问题的场景:


<Control 属性A="{Binding Path=路径}">
<Control.一个自定义附加属性(记作DP)>
<附加属性的值 属性B="{Binding Path=路径}"/>
</Control.一个自定义附加属性(记作DP)>
</Control>


在这里的问题是:
1.属性A绑定正常,属性B绑定失败,数据源和绑定目标都是依赖属性,父元素设置了DataContext
2.属性B不能绑定到其他任何元素(也就是DP标签之外,包括Control)的属性,即便使用ElementName明确指定数据源也是如此,但是DP标签内部的对象属性可以正常绑定

请教下各位大神,这是怎么回事?谢谢大家,感激不尽
...全文
358 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
q199308040000 2018-10-11
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
[quote=引用 4 楼 q199308040000 的回复:]
[quote=引用 3 楼 duanzi_peng 的回复:]
把你的代码贴全了

那我就直接贴代码,不贴伪代码了

<Grid>
<Button>
<Beh:BehaviorList.BehaviorSingle>
<Beh:BehComTrigger ComParameters="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Tag}"/>
</Beh:BehaviorList.BehaviorSingle>
</Button>
</Grid>
[/quote]
RelativeSource 中的Mode 没有设置[/quote]
大神,设置AncestorType会把Mode隐式设置为FindAncestor
exception92 2018-10-10
  • 打赏
  • 举报
回复
引用 4 楼 q199308040000 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:]
把你的代码贴全了

那我就直接贴代码,不贴伪代码了

<Grid>
<Button>
<Beh:BehaviorList.BehaviorSingle>
<Beh:BehComTrigger ComParameters="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Tag}"/>
</Beh:BehaviorList.BehaviorSingle>
</Button>
</Grid>
[/quote]
RelativeSource 中的Mode 没有设置
exception92 2018-10-09
  • 打赏
  • 举报
回复
把你的代码贴全了
q199308040000 2018-10-09
  • 打赏
  • 举报
回复
引用 1 楼 exception1992的回复:
属性B在属性A内部,搜一下RelativeSource属性的用法试试。
大神,刚才试了下,没用
exception92 2018-10-09
  • 打赏
  • 举报
回复
属性B在属性A内部,搜一下RelativeSource属性的用法试试。
q199308040000 2018-10-09
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
把你的代码贴全了

那我就直接贴代码,不贴伪代码了

<Grid>
<Button>
<Beh:BehaviorList.BehaviorSingle>
<Beh:BehComTrigger ComParameters="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Tag}"/>
</Beh:BehaviorList.BehaviorSingle>
</Button>
</Grid>

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧