开发者

How to make OnRender() draw above the control, not below?

开发者 https://www.devze.com 2023-04-04 14:44 出处:网络
I want to draw underlines in textboxes with the OnRender method but the line is drawn behind the textbox. The textbox is opaque so the underline won\'t be visible. How can I draw something above the t

I want to draw underlines in textboxes with the OnRender method but the line is drawn behind the textbox. The textbox is opaque so the underline won't be visible. How can I draw something above the textbox?

protected override void OnRender(DrawingContext dc){
    dc.DrawLine(new Pen(new LinearGradientBrush(Colors.Green, Colors.Blue, 0.0d), 2), new Point(0, Height - 4), new Point(Width, Height - 4));
}

By the way, why does everyone use "base.OnRender(drawingContext);" in their OnRender() Methods? It does not change anything for me.

I can't use TextDecoration because the underline must be drawn even if there is no text.

Edit:

Might not be a beautiful solution but it seems like开发者_如何学JAVA there is no better way: The OnRender() Method draws the background and after that, the line. The TextBox Background property is set to null so the background won't be drawn again.


Just a gess: Do you tryied to call base.OnRender() before drawing your line ?


in an override like that you'd better always leave the base method call, like in your case

base.OnRender(dc);

if you remove it your override has to draw/render everything and the base class won't render anything. In general depends on usage patterns and scenarios of course but generally removing it is dangerous.

Edit: as for your question, it seems not easy to override/customize the rendering behaviour of WPF TextBox, I found this one:

Customizing WPF TextBox Not Easy, But Possible

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号