【发布时间】:2012-11-14 12:00:01
【问题描述】:
我有一个包含 ItemsSource DependenceProperty 的 UserControl,它必须绑定到内部控件的 ItemsSource 属性:
ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource Self}}"
对
ItemsSource="{Binding ItemsSource, ElementName=controlName}"
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` 是您的 UserControl 的名称吗?
-
更新了答案。你可以试试吗?