我正在写一个标尺控件,我想在它下面画一个刻度.不幸的是,WPFs的DrawingContext.DrawText方法(尽管它被调用了)没有绘制任何东西。有没有人有同样的问题并且知道解决方案?这是代码中呈现文本的部分:
protected void RenderRulerScale(DrawingContext drawingContext)
{
int value = 0;
for (double x = this.LargeUnit * (this.ActualWidth / this.Maximum);
x < this.ActualWidth; x += this.LargeUnit * (this.ActualWidth / this.Maximum))
{
string unitString = (++value).ToString() + this.UnitName;
FormattedText formattedUnitString = new FormattedText(unitString,
new CultureInfo("en-US"), FlowDirection.LeftToRight,
new Typeface("Arial"), 5.0, Brushes.Black)
{
TextAlignment = TextAlignment.Center,
MaxTextWidth = 2 * this.LargeUnit,
MaxTextHeight = 25.0
};
drawingContext.DrawText(formattedUnitString, new Point(x, (2.0 / 3.0) *
this.ActualHeight));
}
}此方法在OnRender方法中调用:
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
this.RenderRulerScale(drawingContext);
}发布于 2012-05-30 17:39:31
我终于找到解决方案了!我认为问题出在文本对齐上。我把它放回左边,它就起作用了。代码如下:
int value = 0;
for (double x = this.LargeUnit * (this.ActualWidth / this.Maximum);
x < this.ActualWidth; x += this.LargeUnit * (this.ActualWidth / this.Maximum))
{
string unitString = (++value).ToString() + this.UnitName;
FormattedText formattedUnitString = new FormattedText(unitString,
Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal,
FontStretches.Normal) , 10.0, Brushes.Black);
drawingContext.DrawText(formattedUnitString,
new Point(x - (formattedUnitString.Width / 2.0), (2.0 / 3.0) *
this.ActualHeight));
}https://stackoverflow.com/questions/10754210
复制相似问题