Reputation: 894
I have the following scenario (I'm using Silverlight4 Beta):
UserControl A: -> DataContext is an object of class MyModel which contains properties Property01 (CustomClass), Property02 (CustomEnum). The MyModel class implements INotifyPropertyChanged and all properties are calling the PropertyChanged event whenever they cange.
UserControl B: -> DataContext must be an object of type (CustomClass) -> DependencyProperty MyProperty of type (CustomEnum)
My UserControlA will contain a UserControlB and I would like to bind the properties in the following way (from XAML):
-> UserControlB.DataContext = UserControlA.DataContext.Property01
-> UserControlB.MyProperty = UserControlA.DataContext.Property02
<!-- DataContext = MyModel -->
<Grid x:Name="LayoutRoot">
<foo:UserControlB x:Name="xpto" DataContext="{Binding Property01}" MyProperty="{Binding Property02}" />
</Grid>
Making things up like above sets the DataContext of UserControlB to the correct value, however the binding on UserControlB.MyProperty never occurs. If I remove the statement DataContext="{Binding Property01}" then the binding on UserControlB.MyProperty WILL occur.
Can someone please explain me if I'm doing something that goes agains the universal laws of Silverlight :-)
Many thanks in advance, Bruno
Upvotes: 0
Views: 125
Reputation: 3721
The way your XAML is setup the binding is currently:
-> UserControlB.DataContext = UserControlA.DataContext.Property01
-> UserControlB.MyProperty = UserControlB.DataContext.Property02
The way that:
MyProperty="{Binding Property02}"
Works is that it's saying Bind to the Property "Property02" on my DataContext, if I don't have a DataContext check my parents DataContext, and up the tree until it finds a DataContext.
It doesn't however continue up the tree if the DataContext doesn't have a property named "Property02"
Upvotes: 1