首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用TextBlock自定义OnRender

如何使用TextBlock自定义OnRender
EN

Stack Overflow用户
提问于 2014-09-07 02:59:23
回答 1查看 908关注 0票数 2

我搞不懂.我开始觉得做不到。

我想创建一个自定义textblock控件,它允许测量包含的文本。由于OnRender()是密封的,所以我无法执行下面的操作。但是,如果使用new,则永远不会调用新的"OnRender()“。那么如何才能做到呢?有更好的办法吗?( CustomTextBlock在MVVM框架中的ItemsControl中使用。我不想创建任何附加的依赖项属性)。

请事先提出建议并表示感谢。

在XAML中使用:

代码语言:javascript
复制
 <i:CustomTextBlock Grid.Row="0"  x:Name="tbc"
               Text="{Binding Text}" 
               FontSize="{Binding FontSize}" 
               FontStyle="Italic" 
               Foreground="{Binding Color}" 
               FontFamily="Segoe Script" />

控制定义:

代码语言:javascript
复制
 public class CustomTextBlock : TextBlock
{
    public CustomTextBlock() : base()
    {
    }

 // THIS FAILS...BUT "NEW" IS NEVER CALLED???
    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        Typeface typeface = new Typeface(this.FontFamily,
            this.FontStyle,
            this.FontWeight,
            this.FontStretch); 

        FormattedText formmatedText = new FormattedText(
            this.Text,
            System.Threading.Thread.CurrentThread.CurrentCulture,
            this.FlowDirection,
            typeface,
            this.FontSize,
            this.Foreground);

    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-09-07 05:19:02

无论如何,我无法在OnRender中找到答案,但这解决了我的问题。这是一个可能的解决办法,虽然不是一个真正的答案。

在UserControl的代码背后,XAML用x:Name="tbc“定义TextBlock。

希望这能帮上忙。

代码语言:javascript
复制
   public partial class InkStringView : UserControl
{
    public InkStringView()
    {
        InitializeComponent();           
        tbc.Loaded += new RoutedEventHandler(InkStringView_Loaded);
    }

    void InkStringView_Loaded(object sender, RoutedEventArgs e)
    {
        TextBlock tb = sender as TextBlock;

        Typeface typeface = new Typeface(
          tb.FontFamily,
          tb.FontStyle,
          tb.FontWeight,
          tb.FontStretch);

       FormattedText formattedText = new FormattedText(
            tb.Text,
            System.Threading.Thread.CurrentThread.CurrentCulture,
            tb.FlowDirection,
            typeface,
            tb.FontSize,
            tb.Foreground);

        StringViewModel svm = tb.DataContext as StringViewModel;
        svm.Bounds = formattedText.GetBoundingRect();
        svm.MidlineY1 = svm.MidlineY2 =  svm.Bounds.Top + 0.45 * (formattedText.Baseline - svm.Bounds.Top);


        double width = tb.ActualWidth;
        double height = tb.ActualHeight;

        var parent = VisualTreeHelper.GetParent(this);
        while (!(parent is Window))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }


        GeneralTransform gt = tb.TransformToAncestor(parent as UIElement);
        Point offset = gt.Transform(new Point(0, 0));
        double controlTop = offset.Y;
        double controlLeft = offset.X;
    }       
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25706684

复制
相关文章

相似问题

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