wpf scrollviewer+网格和VisualTreeHelper.getChildRencount()

发布于 2025-01-22 07:28:07 字数 2980 浏览 3 评论 0原文

因此,我试图从父级控制( maingrid )中获取所有控件,因此我使用了此操作:

            public static List<Visual> GetChildrens(Visual iParent, bool iRecursive)
            {
                List<Visual> result = new List<Visual>();

                iParent.Dispatcher.Invoke((Action)(() =>
                {
                    if (iRecursive)
                    {
                        Queue<Visual> toCheck = new Queue<Visual>();

                        toCheck.Enqueue(iParent);

                        while (toCheck.Count > 0)
                        {
                            for (int j = 0; j < VisualTreeHelper.GetChildrenCount(toCheck.Peek()); j++)
                            {
                                Visual childVisual = (Visual)VisualTreeHelper.GetChild(toCheck.Peek(), j);

                                toCheck.Enqueue(childVisual);
                                result.Add(childVisual);
                            }

                            toCheck.Dequeue();
                        }
                    }
                    else
                    {
                        for (int j = 0; j < VisualTreeHelper.GetChildrenCount(iParent); j++)
                        {
                            Visual childVisual = (Visual)VisualTreeHelper.GetChild(iParent, j);
                            result.Add(childVisual);
                        }
                    }
                }));

                return result;
            }

它可以与 grid stackpanel stackpanel + 网格 scrollviewer ,但不要 scrollviewer + grids grids

    <Grid x:Name="MainGrid">
        <ScrollViewer>
            <Grid>
                <Label Content="Pizza0" x:Name="Test0_Localize_Content" />
                <!--Don't work-->
            </Grid>
        </ScrollViewer>

        <ScrollViewer x:Name="SVName">
            <Grid>
                <Label Content="Pizza1" x:Name="Test1_Localize_Content" /> 
                <!--Don't work-->
            </Grid>
        </ScrollViewer>

        <ScrollViewer>
            <Label Content="Pizza0" x:Name="Test0_Localize_Content" />
            <!--Work-->
        </ScrollViewer>

        <Grid>
            <Grid>
                <Label Content="Pizza2" x:Name="Test2_Localize_Content" /> 
                <!--Work-->
            </Grid>
        </Grid>

        <StackPanel>
            <Grid>
                <Label Content="Pizza3" x:Name="Test3_Localize_Content" /> 
                <!--Work-->
            </Grid>
        </StackPanel>
    </Grid>

什么我不明白? (因此要发布我的问题:“看起来您的帖子主要是代码;请添加更多细节”)因此:我如何解决它,我不了解它是WPF和C#。

So I'm trying to get all controls from parent control (MainGrid) for it i use this:

            public static List<Visual> GetChildrens(Visual iParent, bool iRecursive)
            {
                List<Visual> result = new List<Visual>();

                iParent.Dispatcher.Invoke((Action)(() =>
                {
                    if (iRecursive)
                    {
                        Queue<Visual> toCheck = new Queue<Visual>();

                        toCheck.Enqueue(iParent);

                        while (toCheck.Count > 0)
                        {
                            for (int j = 0; j < VisualTreeHelper.GetChildrenCount(toCheck.Peek()); j++)
                            {
                                Visual childVisual = (Visual)VisualTreeHelper.GetChild(toCheck.Peek(), j);

                                toCheck.Enqueue(childVisual);
                                result.Add(childVisual);
                            }

                            toCheck.Dequeue();
                        }
                    }
                    else
                    {
                        for (int j = 0; j < VisualTreeHelper.GetChildrenCount(iParent); j++)
                        {
                            Visual childVisual = (Visual)VisualTreeHelper.GetChild(iParent, j);
                            result.Add(childVisual);
                        }
                    }
                }));

                return result;
            }

And it work with Grid, StackPanel, StackPanel + Grid, ScrollViewer but don't with ScrollViewer + Grids:

    <Grid x:Name="MainGrid">
        <ScrollViewer>
            <Grid>
                <Label Content="Pizza0" x:Name="Test0_Localize_Content" />
                <!--Don't work-->
            </Grid>
        </ScrollViewer>

        <ScrollViewer x:Name="SVName">
            <Grid>
                <Label Content="Pizza1" x:Name="Test1_Localize_Content" /> 
                <!--Don't work-->
            </Grid>
        </ScrollViewer>

        <ScrollViewer>
            <Label Content="Pizza0" x:Name="Test0_Localize_Content" />
            <!--Work-->
        </ScrollViewer>

        <Grid>
            <Grid>
                <Label Content="Pizza2" x:Name="Test2_Localize_Content" /> 
                <!--Work-->
            </Grid>
        </Grid>

        <StackPanel>
            <Grid>
                <Label Content="Pizza3" x:Name="Test3_Localize_Content" /> 
                <!--Work-->
            </Grid>
        </StackPanel>
    </Grid>

What i don't understand? (So to post my question "It looks like your post is mostly code; please add some more details") so: how i can fix it, what i don't understand it's WPF and c#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2025-01-29 07:28:07

ScrollViewer控件没有孩子的定义,因为它只能容纳一个元素。

要使ScrollViewer的孩子您必须检查其内容。

对您的代码的最简单修改是:

while (toCheck.Count > 0)
{
    if(toCheck.Peek() is ScrollViewer)  
    {
        ScrollViewer scroll = toCheck.Peek() as ScrollViewer;
        if(scroll.Content is not null)
        {
            toCheck.Enqueue(scroll.Content as Visual);
            result.Add(scroll.Content as Visual);
        }
    }
    else
    {
        for (int j = 0; j < VisualTreeHelper.GetChildrenCount(toCheck.Peek()); j++)
        {
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(toCheck.Peek(), j);

            toCheck.Enqueue(childVisual);
            result.Add(childVisual);
        }
    }

    toCheck.Dequeue();
} 

The ScrollViewer control doesn't have a definition of children because it can only hold one element.

To get the child of the ScrollViewer you have to check its Content.

the easiest modify to your code is:

while (toCheck.Count > 0)
{
    if(toCheck.Peek() is ScrollViewer)  
    {
        ScrollViewer scroll = toCheck.Peek() as ScrollViewer;
        if(scroll.Content is not null)
        {
            toCheck.Enqueue(scroll.Content as Visual);
            result.Add(scroll.Content as Visual);
        }
    }
    else
    {
        for (int j = 0; j < VisualTreeHelper.GetChildrenCount(toCheck.Peek()); j++)
        {
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(toCheck.Peek(), j);

            toCheck.Enqueue(childVisual);
            result.Add(childVisual);
        }
    }

    toCheck.Dequeue();
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文