添加标签来自内部的用户控件的tabcontrol [英] adding tabs to tabcontrol from inside usercontrol

查看:234
本文介绍了添加标签来自内部的用户控件的tabcontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能添加选项卡,在一个用户控件存在从包含一个标签本身内的另一个用户控件一个TabControl?
我可以不用在TabControl的作为参数传递在构造函数中,可能通过一些静态全局方法?



我试过



 公共静态ObservableTabCollection FindCollectionFromUC(此DependencyObject depObject)
{
布尔循环= TRUE;
VAR父=(VisualTreeHelper.GetParent(depObject)为FrameworkElement的);
,而(循环)
{
如果(parent.GetType()是typeof运算(的TabControl))
{
环= FALSE;
回报率((ObservableTabCollection)((TabControl的)母公司).ItemsSource);
}
父= parent.GetParent()作为FrameworkElement的;
}
返回NULL;
}



==== ====编辑
解决的办法是这样的:

 布尔循环= TRUE; 
VAR父= depObject为FrameworkElement的;

,而(循环)
{
如果(父!= NULL)
{
父= VisualTreeHelper.GetParent(父)为FrameworkElement的;
变种类型= parent.GetType();
如果(parent.GetType()== typeof运算(的TabControl))
{
环= FALSE;
回报率((ObservableTabCollection)((TabControl的)母公司).ItemsSource);
}
}
,否则{循环= FALSE; }
}
返回NULL;


解决方案

在用户控件需要寻找的TabControl的一些手段。你可以通过一个实例,作为一个选项(可能是最强大的)。或者,您可以使用某种形式的依赖注入或服务的检索正确的TabControl。



另一种选择,虽然可能变脆,将是向上导航树,直到你找一个TabControl。 FrameworkElement的(其中用户控件和其他面板派生)定义了一个财产。这将有可能让你走了,发现包含该用户控件的TabControl的实例。


How can I add tabs to a tabcontrol that exists in one usercontrol from another usercontrol that is contained within a tab itself?? Can I do it without passing in the tabcontrol as a parameter in the constructor, perhaps via some static global method?

I've tried

public static ObservableTabCollection FindCollectionFromUC(this DependencyObject depObject)
        {
            bool loop = true;
            var parent = (VisualTreeHelper.GetParent(depObject) as FrameworkElement);
            while (loop)
            {
                if (parent.GetType() is typeof(TabControl))
                {
                    loop = false;
                    return ((ObservableTabCollection)((TabControl)parent).ItemsSource);
                }
                parent = parent.GetParent() as FrameworkElement;
            }
            return null;
        }

==== EDIT ==== The Solution was this:

            bool loop = true;
            var parent = depObject as FrameworkElement;

            while (loop)
            {
                if (parent != null)
                {
                    parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;
                    var type = parent.GetType();
                    if (parent.GetType() == typeof(TabControl))
                        {
                            loop = false;
                            return ((ObservableTabCollection)((TabControl)parent).ItemsSource);
                        }
                }
                else { loop = false; }
            }
            return null;

解决方案

The UserControl will need some means of finding the TabControl. You could pass an instance, as one option (probably the most robust). Alternatively, you could use some form of Dependency Injection or a service to retrieve the correct TabControl.

The other option, though potentially brittle, would be to navigate up the tree until you find a TabControl. FrameworkElement (of which UserControl and other panels derive) defines a Parent property. This would potentially allow you to walk up and find the TabControl instance containing this UserControl.

这篇关于添加标签来自内部的用户控件的tabcontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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