将只读 DependencyProperty 绑定到另一个只读 DependencyProperty

发布于 2024-12-02 18:51:47 字数 359 浏览 2 评论 0原文

我有一个包含只读 DependencyProperty 的类。在这个类中,我想将只读 DependencyProperty 绑定到另一个类的另一个只读 DependencyProperty (从代码创建绑定)。

有可能这样做吗? SetBinding() 似乎没有以 DependencyPropertyKey 作为参数的重载? BindingOperations 类似乎都无法提供此类功能。

这个问题也可以表述为:如果我有 DependencyPropertyKey,我可以为只读 DependencyProperty 创建绑定吗?

I have a class that contains a readonly DependencyProperty. From this class, I want to bind to the readonly DependencyProperty to another readonly DepenendencyProperty of another class (create binding from code).

Is there a possibility to do this? SetBinding() seems not to have a overload that takes a DependencyPropertyKey as a parameter? Neither seems the BindingOperations-class to provide such functionality.

The question can also be formulated: Can I create a binding for a readonly DependencyProperty if I have the DependencyPropertyKey?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

好久不见√ 2024-12-09 18:51:47

这是不可能的,因为 FrameworkElement.SetBinding 方法仅接受 DependencyProperty 作为其第一个参数。如果它也接受 DependencyPropertyKey 就好了。由于该键在类外部不可见,因此它在类外部仍然是只读的,但在类内部您可以将它用于绑定。不幸的是,这是不可能的。

This isn't possible because the FrameworkElement.SetBinding method only accepts a DependencyProperty as its first parameter. It would have been great if it would also accept a DependencyPropertyKey instead. Because the key isn't visible outside the class, it would still be read-only outside the class, but from within the class you could use it for a binding. Unfortunately, this isn't possible.

阪姬 2024-12-09 18:51:47

关于如何实现这一点有什么好的建议吗?

人们可以尝试实现私有 RW 依赖属性,将原始 RO 绑定到它,并在 RW 的回调中修改公开的 RO。但这听起来像是一个黑客。还有更好的选择吗?

Any good suggestions on how to implement this?

One could try to implement a private RW dependency property, bind the original RO to it, and in RW's callback modify the exposing RO. But this sounds like a hack. Any better options?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文