【发布时间】: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 类(来自上面的链接)都在单独的类文件中。
【问题讨论】: