我遇到了非常奇怪的问题。我无法弄清楚如何将放置的属性绑定FooItemsControlViewModel
到 View( FooItemsControl
):
模型:
public interface IFolder
{
string FullPath { get; }
string FolderLabel { get; }
List<IFolder> Folders { get; }
}
视图模型:
private ObservableCollection<IFolder> m_folders;
public ObservableCollection<IFolder> Folders
{
get { return m_folders; }
set
{
m_folders = value;
OnPropertyChanged("Folders");
}
}
XAML。此cDataTemplate
将用于<DataGridTextColumn.HeaderTemplate>
:DataGrid
<DataGrid ItemsSource="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" >
<DataGridTextColumn.HeaderTemplate>
...
<DataTemplate>
...
<PopUp>
<TreeView ItemsSource="{Binding DataContext.Folders, RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, diag:PresentationTraceSources.TraceLevel=High}" Margin="10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding DataContext.FooFolders, RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}}" DataType="{x:Type toDelete:IFolder}">
<TextBlock Text="{Binding FolderLabel}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
...
</PopUp>
</DataTemplate>
...
我已将跟踪添加到输出窗口以调试绑定:
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
我在输出窗口中看到的是:
System.Windows.Data 警告:78:BindingExpression (hash=52875503):使用根项 FooItemsControl 激活 (hash=67071923)
System.Windows.Data 警告:107:BindingExpression (hash=52875503):在级别 0 使用 FooItemsControl.DataContext 的缓存访问器:DependencyProperty(DataContext)
System.Windows.Data 警告:104:BindingExpression (hash=52875503):使用访问器 DependencyProperty(DataContext) 将级别 0 的项目替换为 FooItemsControl (hash=67071923)
System.Windows.Data 警告:101:BindingExpression (hash=52875503): GetValue at level 0 from FooItemsControl (hash=67071923) using DependencyProperty(DataContext): FooItemsControlViewModel (hash=28094463)
System.Windows.Data 警告:107:BindingExpression (hash=52875503):在级别 1 使用 FooItemsControlViewModel.Folders 的缓存访问器:RuntimePropertyInfo(Folders)
System.Windows.Data 警告:104:BindingExpression (hash=52875503):使用访问器 RuntimePropertyInfo(Folders) 将级别 1 的项目替换为 FooItemsControlViewModel (hash=28094463)
System.Windows.Data 警告:101 : BindingExpression (hash=52875503): GetValue at level 1 from FooItemsControlViewModel (hash=28094463) using RuntimePropertyInfo(Folders):
System.Windows.Data 警告:80:BindingExpression (hash=52875503): TransferValue - 得到原始值
System.Windows.Data 警告:89:BindingExpression (hash=52875503): TransferValue - 使用最终值
并且Folders
属性未显示在 中TreeView
。有谁知道我看不到任何价值的原因TreeView
(我之前看到过价值)?