Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DataContext within UserControl is affecting bindings in parent

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?

like image 522
GazTheDestroyer Avatar asked Sep 27 '11 15:09

GazTheDestroyer


2 Answers

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:

<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" 
                       DataContext="{Binding RelativeSource={RelativeSource Self}}"
                       PropertyOnChild="{Binding PropertyInParentContext}"/>
</UserControl>

This is why you do not set the DataContext of UserControls, it will override the inherited DataContext (and even obfuscate the fact that there is a different context). If you want to bind to properties of the UserControl in its declaration then name the control and use ElementName or RelativeSource-bindings instead.

like image 122
H.B. Avatar answered Oct 08 '22 05:10

H.B.


Self means the UserControl, so when you set the DataContext to Self, you are setting the DataContext to the UserControl 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 to Self.DataContext, you would essentially be creating a loop where a value is bound to itself.

like image 43
Rachel Avatar answered Oct 08 '22 05:10

Rachel