I ve got some code which sets up a datacontext. Often enough, the datacontext should be set to some underlying data collection, such as an ObservableCollection
- but occasionally I d like to set it to a collection that is itself a dependency property.
This can be done in xaml, but doing so defeats the purpose, which is to share the UI code between both scenarios.
Let s say I have a dependency property:
public static readonly DependencyProperty MyDataProperty = [whatever];
and elsewhere, I have a control that expects me to setup the datacontext:
myGreatControl.DataContext = ???
How can I set the above datacontext to refer to the collection stored in the dependency property?
The following question seems related: Silverlight: Programmatically binding control properties
But I d like not to bind one property to another, but a property to a datacontext. The advantage of that is that I don t need to know the type or name or even purpose at the binding code - any FrameworkElement
has a datacontext, and I have an (updateable) property I d like to bind to it.