如何获得在一个内部使用的面板 TreeView?默认情况下我已经阅读了 TreeView 使用 VirtualizingStackPanel 为了这。当我看着 TreeView 模板,我所看到的只是u003CItemsPresenter />,这似乎隐藏了使用的面板的细节。

可能的解决方案:

1)在TreeView实例(“ TV”)上,从代码中执行此操作:TV.ItemSpanel。

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

2)制作一个明确替换的树景模板u003CItemsPresenter />使用您自己的项目scontrol.itemspanel。无论如何,我都提供一个特殊的模板,所以在我的情况下这很好。然后给您放置在该模板中的面板上的零件名称,然后从代码中获得该部分(即面板)。这个问题?见下文。

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

<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>

我在这里剥离了所有混乱以获得可见性...

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

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

有帮助吗?

解决方案

有一个名为 VisualTreeHelper 这使您可以获得对控件的非暴露儿童控制/元素。

这是指向 GetChild 该课程的方法使您可以枚举所有孩子,等等:

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

其他提示

以下是David上面给出的答案的补充。这提供了一种让所有视觉孩子的方法 UIElement通过扩展方法。

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top