首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPFs DrawingContext.DrawText无法绘制

WPFs DrawingContext.DrawText无法绘制
EN

Stack Overflow用户
提问于 2012-05-25 20:12:56
回答 1查看 1.1K关注 0票数 0

我正在写一个标尺控件,我想在它下面画一个刻度.不幸的是,WPFs的DrawingContext.DrawText方法(尽管它被调用了)没有绘制任何东西。有没有人有同样的问题并且知道解决方案?这是代码中呈现文本的部分:

代码语言:javascript
复制
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方法中调用:

代码语言:javascript
复制
protected override void OnRender(DrawingContext drawingContext)
{
    base.OnRender(drawingContext);
    this.RenderRulerScale(drawingContext);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-30 17:39:31

我终于找到解决方案了!我认为问题出在文本对齐上。我把它放回左边,它就起作用了。代码如下:

代码语言:javascript
复制
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));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10754210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档