【问题标题】:How can I loop through all of the controls in a tab control (wpf)?如何遍历选项卡控件 (wpf) 中的所有控件?
【发布时间】:2010-02-25 15:50:57
【问题描述】:

我已经看到了一些如何使用 winforms 执行此操作的示例,但由于 wpf TabItem 没有控件的定义,因此无法使其在 wpf 中工作。这是我现在正在使用的代码,它不起作用。

  TabItem ti = rep1Tab;
                var controls = ti.Controls;
                foreach (var control in controls)
                {
                    //do stuff
                }

【问题讨论】:

    标签: wpf foreach c#-4.0 tabitem


    【解决方案1】:

    TabItem 通常包含一个容器控件,例如默认的 Grid。您可以尝试遍历该容器控件的子项。

    foreach (UIElement element in Grid1.Children)
            {
                //process element
            }
    

    如果必须访问特定控件的属性,则必须转换元素

     foreach (UIElement element in Grid1.Children)
            {
                //process element
                Button btn = (Button)element;
                btn.Content = "Hello World";
            }
    

    【讨论】:

      【解决方案2】:

      如果您想要逻辑子代,请使用LogicalTreeHelper.GetChilren()。 如果你想要视觉孩子,你可以使用 VisualTreeHelper.GetChild()VisualTreeHelper.GetChildrenCount()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多