更改ItemsSource后,TreeViewItem.ItemContainerGenerator返回null

时间:2017-03-07 11:36:41

标签: wpf itemsource

在TreeViewItem_OnExpanded事件中,我有以下代码。

var expandedTreeViewItem = e.OriginalSource as TreeViewItem;
var child = expandedTreeViewItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
expandedTreeViewItem.ItemsSource = // Update ItemsSource
var child = expandedTreeViewItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

在更改扩展树视图项的ItemSource之前获取子项时没有问题,但在更改ItemSource之后,child将为null。

ContainerFromItem(item)中的项目相同,并且在更改ItemSource之前和之后存在。代码的其他部分正常工作。在ItemSource更改后,我该怎么做才能获取元素?

TreeViewItem_OnExpanded事件代码

private void TreeViewItem_OnExpanded(object sender, RoutedEventArgs e)
    {
        var expandedTreeViewItem = e.OriginalSource as TreeViewItem;

        var directoryInfo = expandedTreeViewItem?.Items[0] as DirectoryInfoModel;

        if (expandedTreeViewItem == null || directoryInfo?.HasChild == false) return;

        _folderExplorerViewModel.GetDirectories(directoryInfo?.DirectoryInfo, directoryInfo?.SubDirectoriesCollection);

        expandedTreeViewItem.ItemsSource = directoryInfo?.SubDirectoriesCollection;

        var expandedItemHeader = expandedTreeViewItem?.Template.FindName("PART_Header", expandedTreeViewItem) as ContentPresenter;
        var expandedItemCheckBox = expandedItemHeader?.ContentTemplate.FindName("CheckBoxFolderItem", expandedItemHeader) as CheckBox;

        foreach (var item in expandedTreeViewItem.Items)
        {
            var childItem = expandedTreeViewItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
            var childHeader = childItem?.Template.FindName("PART_Header", childItem) as ContentPresenter;
            var childCheckBox = childHeader?.ContentTemplate.FindName("CheckBoxFolderItem", childHeader) as CheckBox;

            if (childCheckBox != null && expandedItemCheckBox != null)
            {
                childCheckBox.IsChecked = expandedItemCheckBox.IsChecked;
            }
        }
    }
在ItemesSource更改后使用它时,

childItem始终为null。但在此之前,它确实有效。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我找到了答案here。在TreeViewItem_OnExpanded事件未完成其作业之前,尚未在UI中创建新项目,因此ItemContainerGenerator返回null。我在其类中创建了expandedTreeViewItem,并为每个展开的树视图项添加了LayoutUpdated事件处理程序,然后在LayoutUpdated事件中,我可以访问由ItemSource添加的新项。我不知道它是否是一种标准方式,但现在它的工作没有任何返回null。

private TreeViewItem _expandedTreeViewItem;

    private void TreeViewItem_OnExpanded(object sender, RoutedEventArgs e)
        {
           _expandedTreeViewItem = e.OriginalSource as TreeViewItem;

        var directoryInfo = _expandedTreeViewItem?.Items[0] as DirectoryInfoModel;

        if (_expandedTreeViewItem == null || directoryInfo?.HasChild == false) return;

        _folderExplorerViewModel.GetDirectories(directoryInfo?.DirectoryInfo, directoryInfo?.SubDirectoriesCollection);

        _expandedTreeViewItem.ItemsSource = directoryInfo?.SubDirectoriesCollection;

        _expandedTreeViewItem.LayoutUpdated += ExpandedTreeViewItem_OnLayoutUpdated;      
 }

private void ExpandedTreeViewItem_OnLayoutUpdated(object sender, EventArgs e)
        {
            var expandedItemHeader = _expandedTreeViewItem?.Template.FindName("PART_Header", _expandedTreeViewItem) as ContentPresenter;
            var expandedItemCheckBox = header?.ContentTemplate.FindName("CheckBoxFolderItem", header) as CheckBox;

        foreach (var item in _expandedTreeViewItem.Items)
        {
            var childItem = _expandedTreeViewItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
            var childHeader = childItem?.Template.FindName("PART_Header", childItem) as ContentPresenter;
            var childCheckBox = childHeader?.ContentTemplate.FindName("CheckBoxFolderItem", childHeader) as CheckBox;

            if (childCheckBox != null && expandedItemCheckBox != null)
            {
                childCheckBox.IsChecked = expandedItemCheckBox.IsChecked;
            }
        }
}