I have a basic UserControl
that sets its DataContext
to itself for ease of binding:
<UserControl x:Class="MyControlLib.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
</UserControl>
This is used in a parent XAML file like this:
<UserControl x:Class="MyControlLib.ParentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ctrl="clr-namespace:MyControlLib">
<ctrl:ChildControl x:Name="ChildName"
PropertyOnChild="{Binding PropertyInParentContext}"/>
</UserControl>
For some reason, this gives a binding error that seems to indicate that the DataContext
of the parent control is getting affected by the child control setting its own DataContext
.
System.Windows.Data Error: 40 : BindingExpression path error: 'PropertyInParentContext' property not found on 'object' ''ChildControl' (Name='ChildName')'. BindingExpression:Path=PropertyInParentContext; DataItem='ChildControl' (Name='ChildName'); target element is 'ChildControl' (Name='ChildName'); target property is 'PropertyOnChild' (type 'whatever')
Why is "PropertyInParentContext" being looking for in the child control rather than in the parent's DataContext
?
If I remove the
DataContext="{Binding RelativeSource={RelativeSource Self}}
from the child control, then things operate how I would expect.
Am I missing something obvious here?
The declaration of your control and the instantiation are basically manipulating the same object, all the properties that are set in the declaration are also set on every instance. So if the properties were "visible" so to speak:
This is why you do not set the
DataContext
ofUserControls
, it will override the inheritedDataContext
(and even obfuscate the fact that there is a different context). If you want to bind to properties of theUserControl
in its declaration then name the control and useElementName
orRelativeSource
-bindings instead.Self
means theUserControl
, so when you set theDataContext
toSelf
, you are setting theDataContext
to theUserControl
object.The correct syntax for binding to a Control's
DataContext
would be{Binding RelativeSource={RelativeSource Self}, Path=DataContext}
, however since the DataContext is inherited by the Parent, this binding is totally unncessary in any situation.Also, if you bind your
DataContext
toSelf.DataContext
, you would essentially be creating a loop where a value is bound to itself.