What is the DataContext of a DependencyObject in Silverlight4?
I have read that SL4 introduces the ability to data bind properties on objects that derive from DependencyObjects, where previously data-binding only worked on FrameworkElements or FrameworkContentElements.
However, I am not clear on how the binding source is determined when binding properties of DependencyObjects.
In the case of Framew开发者_如何学PythonorkElements, the element's DataContext property is the source object ('walking up the tree' to find a DataContext, if the DataContext isn't set directly).
In the case of DependencyObjects, I would guess that the DataContext used is the DataContext of the 'containing' FrameworkElement in the XAML file. But what is the mechanism for determining this containing object?
In my particular case, I am trying to bind the property of a DependencyObject that lives in an ObservableCollection that is a property of a FrameworkElement. Unfortunately attempting to bind the property on the DependencyObject fails, as the databinding system appears to be using the DependencyObject itself as its own DataContext. It complains (in the output window) that the type does not have a property with the name specified in the binding expression. Binding a dependency property of a FrameworkElement in the same UserControl with the same binding expression is successful.
Have you tried stating the Source or ElementName property when defining the Binding?
(e.g: {Binding Source={StaticResource theFrameworkElement} Path=theObservableCollection[0]}
or {Binding ElementName=theFrameworkElement Path=theObservableCollection[0]}
精彩评论