在选项卡项中查找用户控件的子项 [英] Find children of a user control, in a tab item

查看:23
本文介绍了在选项卡项中查找用户控件的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与查找用户控件的子控件相关的问题.用户控件驻留在选项卡控件的选项卡项中

I have an issue related to finding children of a user control. The user control resides in a tab item of a tab control

XAML

<TabControl HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="150">
    <TabItem Header="First tab">
        <Grid Background="#FFE5E5E5"/>
    </TabItem>
    <TabItem Header="Tab with stackpanel" x:Name="tabWithStackPanel">
        <StackPanel>
            <TextBox></TextBox>
            <TextBox></TextBox>
            <TextBox></TextBox>
            <TextBox></TextBox>
            <TextBox></TextBox>
            <TextBox></TextBox>
            <TextBox></TextBox>
        </StackPanel>
    </TabItem>
    <TabItem Header="Tab with user control" x:Name="tabWithUserControl">
        <control:UserControl1/>
    </TabItem>
</TabControl>
<Button Height="46" Width="70" Panel.ZIndex="1001" Click="Button_Click">Find</Button>

返回依赖对象子对象的方法

The method that return the children of a dependency object

public static List<T> FindChildren<T>(DependencyObject parent) where T : DependencyObject
{
    if (parent == null) return null;

    List<T> children = new List<T>();

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        T childType = child as T;
        if (childType == null)
        {
            children.AddRange(FindChildren<T>(child));
        }
        else
        {
            children.Add((T)child);
        }
    }

    return children;
}

正如您在 XAML 中看到的,第二个 TabItem 包含一个 StackPanel,其中包含一些 TextBoxes.第三个 TabItem 包含一个 UserControl,它也包含 TextBoxes.

So as you see in XAML the second TabItem contains a StackPanel which contains some TextBoxes. The third TabItem contains a UserControl which also contains TextBoxes.

现在,当我单击查找"按钮时,事件处理程序应执行以下操作

Now when I click the Find button the event handler should do the following

    var children1 = Util.FindChildren<TextBox>(tabWithStackPanel.GetValue(TabItem.ContentProperty) as StackPanel);
    var children2 = Util.FindChildren<TextBox>(tabWithUserControl.GetValue(TabItem.ContentProperty) as UserControl1);

问题是第一行返回了 StackPanel 面板的所有子项,但第二行没有返回 UserControl1 的所有子项.

The issue is that first line returns all the children of the StackPanel panel, but the second line does not return all the children of the UserControl1.

为了获得 UserControl1 的所有子项,我必须先选择带有用户控件的选项卡".

I have to select the "Tab with user control" first in order to get all the children of the UserControl1.

知道如何解决问题吗?

推荐答案

未选中的 TabItem 在 Visual 树中不存在,但在 Logical 树中存在.

The non-selected TabItem doesn't exist in the Visual tree, but does exist in the Logical tree.

将您对 VisualTreeHelper 的调用替换为对 LogicalTreeHelper

Replace your calls to VisualTreeHelper with equivalent calls to LogicalTreeHelper

这篇关于在选项卡项中查找用户控件的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆