2010-08-03 126 views

回答

0

的WinForms这是很容易...只是抢的WinForms容器,然后将概率” .Controls'属性和遍历控件集合返回。

    foreach (System.Windows.Forms.Control ctrl in form.Controls) 
        {       
         if (ctrl.Name == "tabPageControl") 
         { // do something with 'tabPageControl object' } 
        { 

正如你可以看到的WinForms其死很容易在全球集装箱返回一个“的ControlCollection”,然后通过迭代,甚至更深如果面板或类似的东西来获得访问权。一旦你找到你想要的只是建立一个可以找到的东西的列表,然后对你的列表或你的控件做些什么。

WPF,这有点不同。我没有丰富的经验,WPF,但之后打约我15分钟想出了这一点:

  private void button1_Click(object sender, RoutedEventArgs e) 
      { 
       // cast out Grid object. 
       Grid grd = (Grid) this.Content; 
       // do simple testing to find out what the type is. 
       string s = grd.ToString(); 
       // in VS, in debug mode, hover 'grd.Children' and Smart Tool Tip that pops 
       // it will tell exactly under a 'count' property how many controls there are sitting 
       // on the global container. For me it was just 1, my Button. 
       foreach (UIElement child in grd.Children) 
       { 
       // do some more testing to make sure have got the right control. pref in an If statement but anyhooo. 
       String sss = child.GetType().FullName; 
       // cast out the appropriate type. 
       Button myWpfButton = (Button)child; 
       } 
      } 

我希望这就是足够多的让你开始。

0

这取决于父控制的类型。如果它是ContentControl的扩展,它只能有一个在Content属性下找到的子元素。 如果它是Panel的扩展,它可以有许多子元素,它们在Children属性下找到。

没有保证,任何这些子元素都一定控制,但 - 你需要做某种类型的检查,以确认它们是否是你感兴趣的类型

这仅是也。对于单一层次的父子层次结构,但是应该足够简单,以便在需要所有子控件时进行递归。