【问题标题】:Combining ClearAll with Watermark on textbox in WPF在WPF中将ClearAll与文本框上的水印结合起来
【发布时间】:2011-11-09 16:52:51
【问题描述】:

在尝试了多种方法让水印为我工作后,我终于在此页面上找到了@Beej 修改的方法: Watermark / hint text / placeholder TextBox in WPF

我把它放在我的项目中,它工作正常,只有一个例外。我在选项卡控件的每个选项卡上有多个文本框。底部是一个清除按钮,用于清除选项卡上的所有文本框。清除按钮工作正常,水印工作正常,但我无法让它们一起工作。窗口加载到位的水印,然后按清除按钮清除所有框,但是直到我通过文本框(每个都获得和失去焦点)之后水印才会重新出现。我已经尝试了很多方法来解决这个问题,比如在按钮 MouseUp 事件中对 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);
            }
        }
    }
}

ChildControls 类和 TextboxWatermarked 类(来自上面的链接)都在单独的类文件中。

【问题讨论】:

    标签: c# wpf textbox watermark


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 2020-02-03
      相关资源
      最近更新 更多