WPF 绑定到另一个对象的 DependencyProperty?

发布于 2024-08-18 06:15:10 字数 580 浏览 10 评论 0原文

我正在开发一个类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2024-08-25 06:15:10

如果通过调试器运行时应用程序的跟踪中确实没有绑定错误,则问题可能出在更改通知中。您必须确保 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 your Group object provides change notifications when changed.

This is usually done by implementing INotifyPropertyChanged on the class, and in the set accessor raising a PropertyChanged event (if the value actually changed).

挽你眉间 2024-08-25 06:15:10

问题可能出在我的DiagramNode 类的Group 对象的属性声明中吗?

Public Class DiagramNode
...
Private _group As Group
Public Property Group() As Group
    Get
        Return Me._group 
    End Get
    Set(ByVal value As Group)
        Me._group = value
    End Set
End Property
...
End Class

Is the issue perhaps in my property declaration of the Group object of my DiagramNode class?

Public Class DiagramNode
...
Private _group As Group
Public Property Group() As Group
    Get
        Return Me._group 
    End Get
    Set(ByVal value As Group)
        Me._group = value
    End Set
End Property
...
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文