Get the value of an AttachedProperty of a UI item not (yet) displayed
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 va开发者_开发问答lue ...
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 ...
I discovered that (in my case at least), the Binding
was Unattached
, but the DataContext
was set.
So I decided to get the DataContext
(the ViewModel
) and to work with it.
Any others suggestions are welcome.
精彩评论