Sep 5, 2013 at 11:25 AM
Edited Sep 5, 2013 at 11:26 AM
how to achieve the same result when creating the UI control in your code behind file?
It depends on when
the control is created: if you are able to generate the control
CM binds the view and the view-model together, you don't need any extra steps. If your view gets modified after being bound to the view-model, you need to force CM perform the binding operation (in other words, you need to call
ViewModelBinder.Bind(viewModel, view, context)
). Note that when I say view, I mean the complete control that was associated to a view-model, not just the control that was added dynamically.
Now, the fact that the binding does not happen unless you add the user control to the visual tree is no surprise: CM navigates the visual tree and uses name scopes to try to retrieve actual controls to be associated to properties. Nnote that I say 'controls',
and not dependency properties... CM binds controls defined in the view, with properties defined in the view-model (and even events to functions).
is it possible then to make use of Caliburn.micro wiring of the binding for non-visual dependency properties in my view?
I think you mean 'is it possible to perform an automatic binding for controls that have not been attached to the visual tree'?
The short answer is: no. Unless CM is able to locate the control in a view name scope, such control will not be eligible to be automatically bound.
Long answer: there could be a possibility hacking with name scopes, so taht CM is able to locate a control outside of the visual tree, but in such a case, I fear you could find quite a lot of problems, due to the fact that bindings rely on the logical/inheritance
tree that is often derived from the visual tree...
That said, your issue has nothing to do with this concepts.
In the first post, you are trying to bind a custom dependency property (defined in a view) to a view-model, while the above discussion is about trying to use CM with controls outside of the visual tree.
For your original purpose, as stated above, you need a custom convention.
Let me be clearer: CM knows that binding a view with a TextBlock called 'Surname' to a view-model having property named 'Surname', means create a binding between the TextBlock.Text property and the view-model 'Surname' property. This is a convention.
In your original example, you have defined a view with a custom property: CM knows nothing about your ListType dependency property, because it has no convention defined for the type SelectionListView. The ConventionManager is used to instruct CM binding engine,
and allow for more complex binding scenarios, on a per-(UIElement-)type basis.