How about using an
Adorner
to give the underline effect.
It goes like this:
Create a class which inherits
Adorner
.
class AdornOnTextBox : Adorner
{
public AdornOnTextBox(UIElement adornedElement):base(adornedElement)
{
}
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Rect adornedElementRect = new Rect(AdornedElement.RenderSize);
Pen pen = new Pen(Brushes.Red, 1);
drawingContext.DrawLine(pen, adornedElementRect.BottomLeft, adornedElementRect.BottomRight);
}
}
Lets say the name of the UserControl in your WPF app is NewTextBox.
Attach an event handler for
GotFocus
.
Add the below in the constructor
NewTextBox.GotFocus += new RoutedEventHandler(TextGotFocus);
NewTextBox.LostFocus += new RoutedEventHandler(TextLostFocus);
AdornOnTextBox adornIt;
AdornerLayer textLayer;
Here are the eventhandlers:
void TextGotFocus(object sender, RoutedEventArgs e)
{
adornIt = new AdornOnTextBox(NewTextBox);
textLayer= AdornerLayer.GetAdornerLayer(NewTextBox);
textLayer.Add(adornIt);
}
void TextLostFocus(object sender, RoutedEventArgs e)
{
textLayer.Remove(adornIt);
}
Hope this works out. :)