我遇到了一个非常奇怪的问题。我无法弄清楚如何将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。这个DataTemplate
cwill将用于<DataGridTextColumn.HeaderTemplate>
的{{1}}:
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>
...
我在“输出”窗口中看到的是:
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):用0替换0级的项目 FooItemsControl(hash = 67071923),使用访问器 DependencyProperty(DataContext)
System.Windows.Data警告:101: BindingExpression(hash = 52875503):来自0级的GetValue 使用DependencyProperty(DataContext)的FooItemsControl(hash = 67071923): FooItemsControlViewModel(hash = 28094463)
System.Windows.Data警告:107:BindingExpression(hash = 52875503):在级别1使用CooItemsControlViewModel.Folders的缓存访问器: RuntimePropertyInfo(文件夹)
System.Windows.Data警告:104:BindingExpression(hash = 52875503):使用访问器替换级别1的项目和FooItemsControlViewModel(hash = 28094463) RuntimePropertyInfo(文件夹)
System.Windows.Data警告:101:BindingExpression(hash = 52875503):来自第1级的GetValue FooItemsControlViewModel(hash = 28094463)使用 RuntimePropertyInfo(文件夹):
System.Windows.Data警告:80: BindingExpression(hash = 52875503):TransferValue - 获得原始值
System.Windows.Data警告:89:BindingExpression (hash = 52875503):TransferValue - 使用最终值
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Folders
属性未显示在TreeView
中。有没有人知道我在TreeView
中看不到任何价值的原因(我之前看到了价值观)?