0

如何获得在一个内使用的面板TreeView?我读过默认情况下为此TreeView使用 a VirtualizingStackPanel。当我查看TreeView模板时,我看到的只是 <ItemsPresenter />,它似乎隐藏了使用哪个面板的详细信息。

可能的解决方案:

1)在树视图实例(“tv”)上,从代码中执行以下操作:tv.ItemsPanel。

问题是,这不会返回一个面板,而是一个 ItemsPanelTemplate(“获取或设置定义控制项目布局的面板的模板”)。

2) 制作一个 TreeView 模板,用您自己的 ItemsControl.ItemsPanel 显式替换 <ItemsPresenter />。无论如何,我提供了一个特殊的模板,所以这在我的场景中很好。然后为您放置在该模板中的面板指定部件名称,并从代码中获取该部件(即面板)。这有什么问题吗?见下文。

(我使用的是从 TreeView 派生的名为 VirtualTreeView 的控件,如下所示):

<ControlTemplate x:Key="VirtualTreeViewTemplate" TargetType="{x:Type local:VirtualTreeView}">
    <Border>
        <local:VirtualScrollViewer 
             Style="{StaticResource VirtualScrollViewer}"
             x:Name="PART_VirtualScrollViewer"
                    CanContentScroll="True">
                <!-- instead of: <ItemsPresenter />, use following: -->
                <ItemsControl>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel
                            Name="PART_ItemsStackPanel"
                            IsItemsHost="True" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
        </local:VirtualScrollViewer>
    </Border>
</ControlTemplate>

[为了能见度,我把这里所有的杂物都去掉了……]

这样做的问题是:这会立即覆盖任何 TreeView 布局机制。实际上,即使您有 TreeViewItems 填充树,您也会得到一个空白屏幕。好吧,我想要掌握面板的原因是参与MeaureOverride,但没有深入了解所有这些,我当然不想重写如何布局树视图的书。即,执行此步骤#2 的方式似乎使甚至首先使用 TreeView 的点无效。

抱歉,如果这里有一些混乱,感谢您提供的任何帮助。

4

2 回答 2

0

有一个名为的类VisualTreeHelper允许您获取控件的非公开子控件/元素。

这是GetChild该类上的方法的链接,它允许您枚举所有孩子等:

http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper.getchild.aspx

于 2010-05-20T19:54:38.473 回答
0

以下是对上面大卫给出的答案的补充。UIElement这提供了一种通过扩展方法获取所有 Visual Children的方法。

public static class WPFXtensions
{
    static public IEnumerable<UIElement> GetDescendantUIElements(this UIElement uiElement)
    {
        int childrenCnt = VisualTreeHelper.GetChildrenCount(uiElement);
        for (int i = 0; i < childrenCnt; i++)
        {
            Visual childVisual = VisualTreeHelper.GetChild(uiElement, i) as Visual;

            if(childVisual == null)
                continue;   

            if (childVisual is UIElement)
            {
                yield return childVisual as UIElement;

                // note: by recursively calling within the loop, we walk the tree all the way down for each
                // UIElement encountered before moving to the next UIElement sibling. 
                foreach (UIElement e in GetDescendantUIElements(childVisual as UIElement))
                    yield return e;
            }
        }
    }
}

 // Use this then like this to retrieve an expected child StackPanel 

StackPanel sp1 = (StackPanel)this.GetDescendantUIElements()
            .First(uiElem => uiElem is StackPanel);

// Get all of this UIElement's descendants (for a diagnostic look); 
// In a run where this was a TreeView, 78 UIElements returned

List<UIElement> uiElements = this.GetDescendantUIElements().ToList();
于 2010-05-21T13:15:27.947 回答