views:

28

answers:

1

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 FrameworkElements, 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.

+1  A: 

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]}

Ozan
That worked. However, I don't need to do this when binding the scale of a ScaleTransform that's set to be the RenderTransform of a StackPanel. I wish I knew how these situations differ.
mackenir
I managed to get data binding working for a dependency object that was a property of a FrameworkElement - the property of the FE must be a dependency property. I also got binding working for DOs in a collection held by a FE - the collection must be a DependencyObjectCollection.I found this out by... re-reading the documentation!: http://msdn.microsoft.com/en-us/library/cc278072(v=VS.95).aspx
mackenir