清除所有文本框的窗口 [英] Clear all TextBox in Window

查看:208
本文介绍了清除所有文本框的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#(WPF)编程。我有很多嵌套控件​​的。我想全部清除我的TextBox控件这是在我的应用程序。这是很难通过他们的名字来访问它们。有什么办法来访问它们递归和清除它们



例如一些事情是这样的:

 公共无效ClearAll(控制C)
{
如果(c是文本框)
{
((文本框)C).Clear();
的回报;
}

的foreach(控制孩子GetChild(C))
{
ClearAll(小孩);
}
}


解决方案

的VisualTreeHelper类来得心应手。您可以使用它是这样的:

 的静态公共无效TraverseVisualTree(视觉myMainWindow)
{
INT childrenCount = VisualTreeHelper.GetChildrenCount(myMainWindow);
的for(int i = 0; I< childrenCount;我++)
{
VAR visualChild =(视频)VisualTreeHelper.GetChild(myMainWindow,I);
如果(visualChild是文本框)
{
文本框TB =(文本框)visualChild;
tb.Clear();
}
TraverseVisualTree(visualChild);
}
}


I'm programming in c#(WPF). I have a lot of nested controls. I want to clear all of my TextBox control which are in my application. It is very hard to access them by their name. Is there any way to access them recursively and clear them?

for example some thing like this:

public void ClearAll(Control c)
{
    if(c is TextBox)
    {
        ((TextBox)c).Clear();
        return;
    }

    foreach(Control child in GetChild(c))
    {
        ClearAll(child);
    }
}

解决方案

The VisualTreeHelper class comes handy. You can use it like this:

static public void TraverseVisualTree(Visual myMainWindow)
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(myMainWindow);
        for (int i = 0; i < childrenCount; i++)
        {
            var visualChild = (Visual)VisualTreeHelper.GetChild(myMainWindow, i);
            if (visualChild is TextBox)
            {
                TextBox tb = (TextBox)visualChild;
                tb.Clear();
            }
            TraverseVisualTree(visualChild);
        }
    }

这篇关于清除所有文本框的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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