1

知道这个话题已经被讨论过很多次了。我想我已经研究了其中的大部分。但我似乎无法使用 AddOwner() 完成这项工作。我是新手。

我希望制作一个非常简单的自定义控件来显示 FormattedText。即使从 XAML 调用依赖项属性,它们也不会设置,并且它们的新设置在构造函数中不可用。

我究竟做错了什么?(也许问题应该是如何正确使用 AddOwner() ?)。

感谢您的任何帮助。

XAML:
<i:CustomTextBlock Grid.Row="0"  x:Name="tbc"
               Text="{Binding Text}" 
               FontSize="{Binding FontSize}" 
               FontStyle="Italic" 
               Foreground="{Binding Color}" 
               FontFamily="Segoe Script" />

C#:

   public class CustomTextBlock : FrameworkElement
   {
    public CustomTextBlock() 
    {
        FontWeight fontweight = FontWeights.Normal;
        FontStretch fontstretch = FontStretches.Normal;
        FlowDirection flowdirection = FlowDirection.LeftToRight;

        Typeface typeface = new Typeface(
         FontFamily,
         FontStyle,
         fontweight,
         fontstretch);

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

        public static readonly DependencyProperty TextProperty = 
        TextBlock.TextProperty.AddOwner(typeof(CustomTextBlock));

       public String Text
    {
        get { return (String)this.GetValue(TextProperty); }
        set { this.SetValue(TextProperty, value); }
    }

       public static readonly DependencyProperty FontSizeProperty =
        TextBlock.FontSizeProperty.AddOwner(typeof(CustomTextBlock));

    public Double FontSize
    {
        get { return (Double)this.GetValue(FontSizeProperty); }
        set { this.SetValue(FontSizeProperty, value); }
    }

    public static readonly DependencyProperty FontStyleProperty =
        TextBlock.FontStyleProperty.AddOwner(typeof(CustomTextBlock));

    public FontStyle FontStyle
    {
        get { return (FontStyle)this.GetValue(FontStyleProperty); }
        set { this.SetValue(FontStyleProperty, value); }
    }

    public static readonly DependencyProperty ForegroundProperty =
        TextBlock.ForegroundProperty.AddOwner(typeof(CustomTextBlock));

    public Brush Foreground
    {
        get { return (Brush)this.GetValue(ForegroundProperty); }
        set { this.SetValue(ForegroundProperty, value); }
    }

    public static readonly DependencyProperty FontFamilyProperty =
        TextBlock.FontFamilyProperty.AddOwner(typeof(CustomTextBlock));

    public FontFamily FontFamily
    {
        get { return (FontFamily)this.GetValue(FontFamilyProperty); }
        set { this.SetValue(FontFamilyProperty, value); }
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
     }

}
4

1 回答 1

1

AddOwner 没有问题,但是属性是在调用构造函数之后设置的。您必须在重写的 OnRender 方法中使用它们的值,并且您可以只删除整个构造函数:

public class CustomTextBlock : FrameworkElement
{
    // property declarations
    ...

    protected override void OnRender(DrawingContext drawingContext)
    {
        var typeface = new Typeface(
            FontFamily,
            FontStyle,
            FontWeights.Normal,
            FontStretches.Normal);

        var formattedText = new FormattedText(
            Text,
            Thread.CurrentThread.CurrentCulture,
            FlowDirection.LeftToRight,
            typeface,
            FontSize,
            Foreground);

        drawingContext.DrawText(formattedText, new Point());
    }
}
于 2014-09-07T19:27:32.287 回答