在WPF的文本框上结合ClearAll和水印
本文关键字:ClearAll 结合 WPF 文本 | 更新日期: 2023-09-27 18:14:07
在尝试了许多方法让水印为我工作后,我终于找到了@Beej在本页上修改的一个:WPF中的水印/提示文本/占位符文本框
我把它放在我的项目中,它工作得很好,只有一个例外。我在标签控件的每个标签上都有多个文本框。底部是一个清除按钮,用于清除选项卡上的所有文本框。清除按钮工作正常,水印工作正常,但我不能让他们一起工作。窗口加载了水印,按下清除按钮清除了所有的框,但是直到我移动了文本框之后,水印才重新出现(每个文本框都有焦点,也有焦点消失)。我已经尝试了许多方法来解决这个问题,例如在按钮MouseUp事件中放置ShowWatermark函数的方法调用,但没有任何工作……帮助?!
这是我使用的Clear按钮方法:
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类(来自上面的链接)都在单独的类文件中
问题不在于您的代码,而在于所选的水印文本框。它只在聚焦或失焦时更新水印,这是一个明显的缺陷。您需要找到一个更好的实现。您尝试过扩展WPF工具包中的那个吗?