在 WPF TreeView 中嵌套数据绑定控件
Nesting databound controls inside a WPF TreeView
正在尝试构建一个可以在其中包含另一个数据绑定控件的 WPF TreeView 控件。这是 XAML:
<TreeView temsSource="{Binding DocumentCategories}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding DocumentCategory1}">
<TextBlock FontWeight="Bold" Text="{Binding Description}"></TextBlock>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Documents}">
<TextBlock FontWeight="Bold" Text="{Binding Name}"></TextBlock>
</ListView>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
DocumentCategories 数据是递归的 - 列表中的每个项目都有一个 DocumentCategory1 集合,DocumentCategories 有自己的 DocumentCategory1 集合,依此类推。
如果里面没有 ListView,这工作得很好。但是,当您添加 ListView 时,TreeView 呈现正常,但是当您尝试打开其中一个节点时,应用程序崩溃并出现错误:
Operation is not valid while ItemsSource is in use. Access and modify
elements with ItemsControl.ItemsSource instead
我不完全确定这是指哪个 ItemsSource - TreeView 或 ListView。我认为是后者,问题是由于在节点打开后绑定才真正发生。
我已经尝试将 DocumentCategories 和 Documents 从 List 更改为 ObservableCollection,这似乎是解决此错误的常见方法 - 但它的行为仍然相同。
是否可以在 TreeView 中有另一个数据绑定控件,如果可以,怎么做?
缺少 ListView 的 DataTemplate。
当前,以下元素被解释为实际的 ListView 元素:
<TextBlock FontWeight="Bold" Text="{Binding Name}"></TextBlock>
并且由于您已经绑定了 ListView 的 ItemSource,因此当视图试图将 "TextBlock" 添加为 ListView 的项目时发生错误。
只需将其更改为以下内容:
<ListView ItemsSource="{Binding Documents}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
正在尝试构建一个可以在其中包含另一个数据绑定控件的 WPF TreeView 控件。这是 XAML:
<TreeView temsSource="{Binding DocumentCategories}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding DocumentCategory1}">
<TextBlock FontWeight="Bold" Text="{Binding Description}"></TextBlock>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Documents}">
<TextBlock FontWeight="Bold" Text="{Binding Name}"></TextBlock>
</ListView>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
DocumentCategories 数据是递归的 - 列表中的每个项目都有一个 DocumentCategory1 集合,DocumentCategories 有自己的 DocumentCategory1 集合,依此类推。
如果里面没有 ListView,这工作得很好。但是,当您添加 ListView 时,TreeView 呈现正常,但是当您尝试打开其中一个节点时,应用程序崩溃并出现错误:
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
我不完全确定这是指哪个 ItemsSource - TreeView 或 ListView。我认为是后者,问题是由于在节点打开后绑定才真正发生。
我已经尝试将 DocumentCategories 和 Documents 从 List 更改为 ObservableCollection,这似乎是解决此错误的常见方法 - 但它的行为仍然相同。
是否可以在 TreeView 中有另一个数据绑定控件,如果可以,怎么做?
缺少 ListView 的 DataTemplate。
当前,以下元素被解释为实际的 ListView 元素:
<TextBlock FontWeight="Bold" Text="{Binding Name}"></TextBlock>
并且由于您已经绑定了 ListView 的 ItemSource,因此当视图试图将 "TextBlock" 添加为 ListView 的项目时发生错误。
只需将其更改为以下内容:
<ListView ItemsSource="{Binding Documents}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>