WPF 绑定到另一个对象的 DependencyProperty?
我正在开发一个类似于 visio 的 WPF 应用程序。我希望能够对图表中的某些项目(每个项目都是一个 UIElement)进行逻辑分组,并在组级别控制某些行为(即可见性)。
我的第一次尝试是创建一个称为“组”的控件,它具有宽度和宽度。 height = 0。我想通过其 group 属性为我的图表元素分配一个特定的“组”,然后将某些 UIElement 属性绑定到该组值,如下所示:
<DiagramNode
Width="300" Height="300"
Visibility="{Binding RelativeSource={RelativeSource Self},Path=Group.Visibility}"
> ... </DiagramNode >
虽然这不会引发绑定错误,但它也不会工作。更改组的可见性不会影响分配给该组的节点的可见性。据我所知,任何时候都不会出现错误,它只是不起作用。
有什么想法吗?我的方法可行吗?如果没有,任何人都有他们想要建议的替代方案:)。我不是一个热衷于 UI 的人,在服务层感觉更舒服,所以我愿意接受其他建议。
I am working on a WPF application similar to visio. I would like to be able to logically group some of the items in my diagram, each of which is a UIElement, and control certain behaviors (i.e. visibility) at the group level.
My first attempt at this was to create a control, called a Group, which had width & height = 0. I wanted to assign to my diagram elements a specific "Group" through their group property, and then bind certain UIElement properties to the group value, as below:
<DiagramNode
Width="300" Height="300"
Visibility="{Binding RelativeSource={RelativeSource Self},Path=Group.Visibility}"
> ... </DiagramNode >
Although this does not throw a binding error, it also doesn't work. Changing the Visibility of the group has no affect on the visibility of the nodes assigned to that group. No errors appear at anytime as far as i can tell, it just doesn't work.
Any ideas? Is my approach possible? If no, any one have alternatives they'd like to suggest :). I'm not a huge UI guy, feel much more comfortable in a service layer, so I'm open to other suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果通过调试器运行时应用程序的跟踪中确实没有绑定错误,则问题可能出在更改通知中。您必须确保
Group
对象的Visibility
属性在更改时提供更改通知。这通常是通过在类上实现
INotifyPropertyChanged
并在 set 访问器中引发PropertyChanged
事件(如果值实际更改)来完成的。If there really is no binding error in the trace of the application when run through the debugger, then the problem is probably in change notifications. You must make sure that the
Visibility
property of yourGroup
object provides change notifications when changed.This is usually done by implementing
INotifyPropertyChanged
on the class, and in the set accessor raising aPropertyChanged
event (if the value actually changed).问题可能出在我的DiagramNode 类的Group 对象的属性声明中吗?
Is the issue perhaps in my property declaration of the Group object of my DiagramNode class?