我具有包含的ItemsSource DependenceProperty必须被绑定到一个内部控制的ItemsSource属性的用户控件:这两种声明自绑定的方式有什么区别?
ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource Self}}"
VS
ItemsSource="{Binding ItemsSource, ElementName=controlName}"
控件名称是控件的名称。
第一个绑定不工作,而第二个工作。我没有区别。
任何想法?
编辑:
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}" >
是您的用户控件的“controlName'叫什么名字? –
@ RV1987:是的。 –
更新了答案。你可以试试吗? –