I have an UserControl containing a ItemsSource DependenceProperty that has to be binded to the ItemsSource property of an internal Control:
ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource Self}}"
vs
ItemsSource="{Binding ItemsSource, ElementName=controlName}"
controlName is the name of the control.
The first binding is not working while the second one works. I don’t get the difference.
Any ideas?
EDIT:
XAML:
<UserControl x:Class="MultiSelectTreeView.MultiSelectableTreeView"
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:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Name="multiTree" >
This does not work ---> <TreeView ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource Self}}" >
This works ---> <TreeView ItemsSource="{Binding ItemsSource, ElementName=multiTree}" >
In case you want to bind to DP of parent UserControl, you need to bind it using
Mode = FindAncestor. Since you are binding on internal control, you need to travel up the Visual Tree.Self Modewill search for DP in internal control and not on parent UserControl.