获取尚未显示的 UI 项目的 AttachedProperty 值
执行以下操作
- 我正在尝试使用 MVVM、Binding 和 AttachedProperty创建 ViewObj (System.Windows.Controls.Control 派生类)
- ViewObj 在名为 View 的类中声明了 1 个名为“Order”(OrderProperty) 的 AttachedProperty。
- 附加属性绑定在 xaml 中 ViewModel 的属性上
- 创建 ViewModel
- 将 ViewModel 设置为 ViewObj 的 DataContext
在显示/渲染 ViewObj 等之前。
在代码中获取订单
var order = View.GetOrder(ViewObj)
ViewObj 已显示并正在显示绑定值...
如果 AttachedProperty 是一个值而不是绑定表达式,则返回的值View.GetOrder(ViewObj)
是一个好的方法,而不是默认的方法。
有什么想法吗?
编辑:
我强制使用 BindingExpression 类来评估数据绑定表达式。我发现 BindingExpression.Status
设置为 Unattached
这似乎解释了为什么它不起作用。
我认为当元素附加到可视树时绑定就附加了。
但是......这对我的问题没有多大帮助......
I'm trying to do the following using MVVM, Binding and AttachedProperty
- Create a ViewObj (System.Windows.Controls.Control derived class)
- The ViewObj has 1 AttachedProperty named "Order" (OrderProperty) declared in a class named View.
- The attached property is bound on a property of the ViewModel in the xaml
- Create the ViewModel
- Set the ViewModel as DataContext of the ViewObj
Before the ViewObj is displayed/rendered/etc.
Get the order in code doing
var order = View.GetOrder(ViewObj)
The ViewObj is displayed and is showing the bound value ...
If the AttachedProperty is a value and not a binding expression, the value returned by View.GetOrder(ViewObj)
is the good one and not the default one.
Any ideas?
EDIT:
I forced the databinding expression to be evaluated using the BindingExpression
class. I discovered that the BindingExpression.Status
was set to Unattached
which seems to explain why it is not working.
I think the binding is attached when the element is attached to the visual tree.
But ... that do not help me a lot with my problem ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现(至少在我的情况下),
Binding
是Unattached
,但DataContext
已设置。因此,我决定获取 DataContext(
ViewModel
)并使用它。欢迎任何其他建议。
I discovered that (in my case at least), the
Binding
wasUnattached
, but theDataContext
was set.So I decided to get the
DataContext
(theViewModel
) and to work with it.Any others suggestions are welcome.