2016-01-30 63 views
0

我想在WPF上的UserControl上繪製一個網格,所以我重寫OnCreate方法,但網格不會在我的UserControl上繪圖。 請幫助:)OnRender方法UserControl問題

public partial class FightField : UserControl 
{ 
    public FightField() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnRender(DrawingContext drawingContext) 
    { 
     base.OnRender(drawingContext); 
     var pen = new Pen(Brushes.Black, 1); 
     for (var i = 1; i < 10; i++) 
     { 
      drawingContext.DrawLine(pen, new Point(this.Height/10 * i, 0), new Point(this.Height/10 * i, this.Width)); 
      drawingContext.DrawLine(pen, new Point(this.Width/10 * i, 0), new Point(this.Width/10 * i, this.Height)); 
     } 
    } 
} 

回答

1

而不是使用this.Heightthis.Width,分別使用this.ActualHeightthis.ActualWidth的。

PS:我試過你的代碼,它只爲我繪製垂直線,所以在邏輯上也存在一些問題。

工作代碼:

drawingContext.DrawLine(pen, new Point(ActualWidth/10 * i, 0), new Point(ActualWidth/10 * i, ActualHeight)); 
drawingContext.DrawLine(pen, new Point(0, ActualHeight/10 * i), new Point(ActualWidth, ActualHeight/10 * i)); 
+0

不客氣! –