将只读 DependencyProperty 绑定到另一个只读 DependencyProperty
我有一个包含只读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,因为
FrameworkElement.SetBinding
方法仅接受DependencyProperty
作为其第一个参数。如果它也接受DependencyPropertyKey
就好了。由于该键在类外部不可见,因此它在类外部仍然是只读的,但在类内部您可以将它用于绑定。不幸的是,这是不可能的。This isn't possible because the
FrameworkElement.SetBinding
method only accepts aDependencyProperty
as its first parameter. It would have been great if it would also accept aDependencyPropertyKey
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.关于如何实现这一点有什么好的建议吗?
人们可以尝试实现私有 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?