C# 将ClearAll与WPF中文本框上的水印相结合

C# 将ClearAll与WPF中文本框上的水印相结合,c#,wpf,textbox,watermark,C#,Wpf,Textbox,Watermark,在尝试了多种方法让水印为我工作后,我终于在本页上找到了@Beej修改的方法: 我把它放在我的项目中,它运行良好,只有一个例外。我在tabcontrol的每个选项卡上都有多个文本框。底部是一个清除按钮,用于清除选项卡上的所有文本框。“清除”按钮工作正常,水印工作正常,但我无法让它们一起工作。窗口加载水印,按“清除”按钮清除所有框,但水印直到我移动文本框(每个文本框都获得和失去焦点)后才会重新出现。我尝试了多种方法来解决此问题,例如在按钮鼠标事件中对ShowWatermark函数进行方法调用,但什

在尝试了多种方法让水印为我工作后,我终于在本页上找到了@Beej修改的方法:

我把它放在我的项目中,它运行良好,只有一个例外。我在tabcontrol的每个选项卡上都有多个文本框。底部是一个清除按钮,用于清除选项卡上的所有文本框。“清除”按钮工作正常,水印工作正常,但我无法让它们一起工作。窗口加载水印,按“清除”按钮清除所有框,但水印直到我移动文本框(每个文本框都获得和失去焦点)后才会重新出现。我尝试了多种方法来解决此问题,例如在按钮鼠标事件中对ShowWatermark函数进行方法调用,但什么都没用…救命

这是我正在使用的清除按钮方法:

    public void ClearTextBoxes()
    {
        ChildControls ccChildren = new ChildControls();

        foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
        {
            if (o.GetType() == typeof(TextBox))
            {
                TextBox txt = (TextBox)o;
                txt.Text = "";
            }

            if (o.GetType() == typeof(DigitBox))
            {
                DigitBox digit = (DigitBox)o;
                digit.Text = "";
            }

            if (o.GetType() == typeof(PhoneBox))
            {
                PhoneBox phone = (PhoneBox)o;
                phone.Text = "";
            }

            if (o.GetType() == typeof(DateBox))
            {
                DateBox date = (DateBox)o;
                date.Text = "";
            }

            if (o.GetType() == typeof(TextBoxWatermarked))
            {
                TextBoxWatermarked water = (TextBoxWatermarked)o;
                water.Text = "";

            }
        }
    }

class ChildControls
{
    private List<object> listChildren;

    public List<object> GetChildren(Visual p_vParent, int p_nLevel)
    {
        if (p_vParent == null)
        {
            throw new ArgumentNullException("Element {0} is null!", p_vParent.ToString());
        }

        this.listChildren = new List<object>();

        this.GetChildControls(p_vParent, p_nLevel);

        return this.listChildren;

    }

    private void GetChildControls(Visual p_vParent, int p_nLevel)
    {
        int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent);

        for (int i = 0; i <= nChildCount - 1; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i);

            listChildren.Add((object)v);

            if (VisualTreeHelper.GetChildrenCount(v) > 0)
            {
                GetChildControls(v, p_nLevel + 1);
            }
        }
    }
}
public void cleartextbox()
{
ChildControls ccChildren=新的ChildControls();
foreach(ccChildren.GetChildren中的对象o(rvraDockPanel,2))
{
if(o.GetType()==typeof(TextBox))
{
TextBox txt=(TextBox)o;
txt.Text=“”;
}
if(o.GetType()==typeof(DigitBox))
{
数字框数字=(数字框)o;
数字。文本=”;
}
if(o.GetType()==typeof(PhoneBox))
{
电话盒电话=(电话盒)o;
phone.Text=“”;
}
if(o.GetType()==typeof(日期框))
{
日期框日期=(日期框)o;
日期。文本=”;
}
if(o.GetType()==typeof(TextBoxWatermarked))
{
TextBoxWatermarked water=(TextBoxWatermarked)o;
水。Text=“”;
}
}
}
类ChildControls
{
儿童私人名单;
公共列表GetChildren(可视p_vParent,int p_nLevel)
{
if(p_vParent==null)
{
抛出新ArgumentNullException(“元素{0}为null!”,pvparent.ToString());
}
this.listChildren=新列表();
这个.GetChildControls(p_vParent,p_nLevel);
归还此文件。列出儿童;
}
私有void GetChildControls(可视p_vParent,int p_nLevel)
{
int nChildCount=VisualTreeHelper.GetChildrenCount(p_vParent);
对于(int i=0;i 0)
{
GetChildControls(v,p_nLevel+1);
}
}
}
}

ChildControls类和TextboxWatermarked类(来自上面的链接)都位于不同的类文件中。

问题不在于代码,而在于所选的带水印文本框。它仅在获得或失去焦点时更新水印,这是一个明显的缺陷。您需要找到更好的实现。你试过里面的那个吗