Reputation: 992
After trying numerous methods to get watermarking to work for me, I finally found the one modified by @Beej on this page: Watermark / hint text / placeholder TextBox in WPF
I got it placed in my project, and it works fine, with one exception. I have multiple textboxes on each tab of a tabcontrol. At the bottom is a clear button that works to clear all the textboxes on the tab. The clear button works fine, the watermark works fine, but I can't get them to work together. The window loads with the watermarks in place, and pressing the clear button clears all the boxes, but the watermarks don't reappear until after I move through the textboxes (each one gaining and losing focus.) I have tried numerous ways to solve this, such as placing a method call to the ShowWatermark function in the button MouseUp event, but nothing has worked...Help?!
This is the Clear button method I'm using:
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);
}
}
}
}
Both the ChildControls class and the TextboxWatermarked class (from the above link) are in seperate class files.
Upvotes: 0
Views: 1020
Reputation: 178680
The problem is not with your code, it's with the chosen watermarked text box. It only updates the watermark when it gains or loses focus, which is an obvious flaw. You'll need to find a better implementation. Have you tried the one in extended WPF toolkit?
Upvotes: 1