为了测试如何在OnRender中的其他控件上绘制,我基于TextBox创建了自己的控件,并决定覆盖它的OnRender方法。但它似乎从来没打过电话。
下面是我得到的简单类:
public class MyTextBox : TextBox
{
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Console.WriteLine("OnRender");
//base.OnRender(drawingContext);
Rect bounds = new Rect(0, 0, 100, 100);
Brush brush = new SolidColorBrush(Colors.Yellow);
drawingContext.DrawRectangle(brush, null, bounds);
}
}我在XAML中声明了这个类:
<local:MyTextBox Height="118" Margin="10,300,10,10" Text="TextBox" VerticalAlignment="Bottom" AcceptsReturn="True" Padding="0,0,200,0" FontSize="18" TextWrapping="Wrap" />但没有迹象表明OnRender打过一次电话。我漏掉了什么?在其他控件上进行自定义绘制的最佳选择是什么?
发布于 2014-02-26 18:13:29
您应该覆盖默认的Textbox样式...
public class MyTextBox : TextBox
{
public MyTextBox()
{
DefaultStyleKey = typeof (MyTextBox);
}
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Console.WriteLine("OnRender");
//base.OnRender(drawingContext);
Rect bounds = new Rect(0, 0, 100, 100);
Brush brush = new SolidColorBrush(Colors.Yellow);
drawingContext.DrawRectangle(brush, null, bounds);
}
}https://stackoverflow.com/questions/22036332
复制相似问题