简体   繁体   English

如何在WPF的TabItem中获取ScrollViewer

[英]How do I get a ScrollViewer inside TabItem In WPF

I need to find a ScrollViewer inside current TabItem and then find a WrapPanel inside that ScrollViewer 我需要在当前TabItem中找到一个ScrollViewer,然后在该ScrollViewer中找到一个WrapPanel

I tried this: 我尝试了这个:

    TabItem ti = tabControl.SelectedItem as TabItem;
        foreach (ScrollViewer sv in ti.Content)
        {
             foreach (WrapPanel wp in sv.Content) {}
        }

and this 和这个

   TabItem ti = tabControl.SelectedItem as TabItem;
        foreach (ScrollViewer sv in ti.Children)
        {
              foreach (WrapPanel wp in sv.Children) {}
        }

But doesn't work 但是不起作用

If your tab item contains your scrollviewer directly, you could do the following : 如果您的标签项直接包含您的scrollviewer,则可以执行以下操作:

TabItem ti = tabControl.SelectedItem as TabItem;
ScrollViewer sv = ti?.Content as ScrollViewer;
WrapPanel wp = scrollViewer?.Content as WrapPanel;

Another way of accessing your WrapPanel would be to use a function that returns a child/content of a specific type. 访问WrapPanel的另一种方法是使用返回特定类型的子项/内容的函数。 For instance 例如

    public T FindVisualChildOrContentByType<T>(DependencyObject parent)
        where T : DependencyObject
    {
        if(parent == null)
        {
            return null;
        }

        if(parent.GetType() == typeof(T))
        {
            return parent as T;
        }

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            if(child.GetType() == typeof(T))
            {
                return child as T;
            }
            else
            {
                T result = FindVisualChildOrContentByType<T>(child);
                if (result != null)
                    return result;
            }
        }

        if(parent is ContentControl contentControl)
        {
            return this.FindVisualChildOrContentByType<T>(contentControl.Content as DependencyObject);
        }

        return null;

    }

Then you would be able to do 那你就可以做

WrapPanel wp = this.FindVisualChildOrContentByType<WrapPanel>(tabItem);

If this is not working, feel free to post your XAML so i can reproduce your exact scenario. 如果这不起作用,请随时发布您的XAML,以便我可以重现您的确切情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使ScrollViewer中的WPF TextBox边框不消失 - How do I make the WPF TextBox border inside the ScrollViewer not disappear 如何在WPF中的TabItem上预呈现控件? - How do I prerender the controls on a TabItem in WPF? 如何将文本框的内容包装在滚动查看器中,以便在 WPF 中满时滚动? - How do I wrap the content of a textbox inside a scrollviewer so that it scrolls when full in WPF? 如何将WPF ScrollViewer的所有内容另存为图像 - How do I save all content of a WPF ScrollViewer as an image 如何在TabItem中获取UserControl? - How to get a UserControl inside a TabItem? 如何获得一个内部有矩形的ScrollViewer,当它到达矩形的末尾时停止滚动? - How do I get a ScrollViewer with a Rectangle inside to stop scrolling when it reaches the end of the rectangle? C# WPF - 如何将垂直滚动查看器放置在水平滚动查看器中 - C# WPF - How to place a vertical scrollviewer inside a horizontal scrollviewer 如何获取TabControl WPF的TabItem标头的宽度 - How to get Width of header of a TabItem of TabControl WPF 如何在不破坏scrollViewer滚动的情况下将ListView和GridView包装在ScrollViewer中 - How do I wrap ListView and GridView inside ScrollViewer without breaking scrollViewer scrolling ScrollViewer 中的 WPF DataGrid - WPF DataGrid inside a ScrollViewer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM