wpf 在选项卡项中查找用户控件的子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16101411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:37:52  来源:igfitidea点击:

Find children of a user control, in a tab item

wpfwpf-controls

提问by Stefan Fachmann

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

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

So as you see in XAML the second TabItemcontains a StackPanelwhich contains some TextBoxes. The third TabItemcontains a UserControlwhich also contains TextBoxes.

因此,正如您在 XAML 中看到的,第二个TabItem包含 a StackPanel,其中包含一些TextBoxes. 第三个TabItem包含 aUserControl也包含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);

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

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

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

我必须首先选择“带有用户控件的选项卡”才能获得UserControl1.

Any clue how to solve the issue?

任何线索如何解决这个问题?

回答by Dean Chalk

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

未选中的TabItem不存在于可视化树中,但存在于逻辑树中。

Replace your calls to VisualTreeHelperwith equivalent calls to LogicalTreeHelper

将您的电话替换为VisualTreeHelper对等的电话LogicalTreeHelper