使用强制时呈现自定义控件失败

本文关键字:自定义控件 失败 | 更新日期: 2023-09-27 18:34:09

在尝试学习强制回调时,我修改了我的自定义文本块(工作正常),以允许将流文档中的段落设置为文本源。我正在尝试使用我的富文本属性来设置 Text 属性并强制重新呈现文本块。

为什么在设置富文本的新值时不调用渲染?(显示未更新)。

感谢您的任何帮助。

top most XAML:
        <v:InkEditorView Height="6.3cm"
        DataContext="{Binding Editor}"
        RichText ="{Binding Note}" />    <--RichText is of type Paragraph
InkEditorView XAML:
      <fsc:InkTextBlock 
         RichText="{Binding RichText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
         FontSize="28" FontFamily="Segoe Script">
    </fsc:InkTextBlock>
 InkEditorView code-behind:
    public Paragraph RichText
    {
        get { return (Paragraph)GetValue(RichTextProperty); }
        set { SetValue(RichTextProperty, value); }
    }
    public static readonly DependencyProperty RichTextProperty =
        DependencyProperty.Register("RichText", typeof(Paragraph), typeof(InkEditorView), new UIPropertyMetadata(null));

控件:

 public class InkTextBlock : FrameworkElement
{
    public FormattedText formattedText;
    // The RichText dependency property is being used to set the Text dependency property. Note the use of Coercion to update
    // the Text property from the RichText property.
    public Paragraph RichText
    {
        get { return (Paragraph)GetValue(RichTextProperty); }
        set { SetValue(RichTextProperty, value); }
    }
    // Using a DependencyProperty as the backing store for RichText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RichTextProperty =
        DependencyProperty.Register("RichText", typeof(Paragraph), typeof(InkTextBlock),
                    new PropertyMetadata(null, OnRichTextPropertyChanged)            
        );
    private static void OnRichTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        InkTextBlock tb = (InkTextBlock)d;
        tb.CoerceValue(TextProperty);           // Calls the Coercion method on the TextProperty
    }

    #endregion
    #region [Text]
        public static readonly DependencyProperty TextProperty = 
        TextBlock.TextProperty.AddOwner(typeof(InkTextBlock),
            new PropertyMetadata(String.Empty, null, OnCoerceText)            
        );
    public String Text
    {
        get { return (String)this.GetValue(TextProperty); }
        set { this.SetValue(TextProperty, value); }
    }
    // This is the Coercion for Text used in the callback. It has access to the RichText property.
    // Return is of type Text --i.e., a string.
    private static object OnCoerceText(DependencyObject d, object baseValue)
    {
        InkTextBlock vm = (InkTextBlock)d;
        Paragraph p = vm.RichText;
        if (p == null) return String.Empty;
        TextRange r = new TextRange(p.ContentStart, p.ContentEnd);
        // TextRange includes the trailing "'r'n"
        String s = r.Text.Trim();
        return s;
    }
    #endregion

     protected override void OnRender(DrawingContext drawingContext)
    {
        var typeface = new Typeface(
            FontFamily,
            FontStyle,
            FontWeights.Normal,
            FontStretches.Normal);
        formattedText = new FormattedText(
            Text,
            Thread.CurrentThread.CurrentCulture,
            FlowDirection.LeftToRight,
            typeface,
            FontSize,
            Foreground);
        // Non-null Decorations will change the size of the formattedText rectangle slightly
        formattedText.SetTextDecorations(Decorations);
        Rect r = formattedText.GetBoundingRect();
        AscenderlineOffset = r.Top;
        BaselineOffset = formattedText.Baseline;
        MidlineOffset = AscenderlineOffset + 0.45 * (BaselineOffset - AscenderlineOffset);
        // Draw the background
        drawingContext.DrawRectangle(Background, new Pen(), r);
         // new Point() just means new Point(0, 0). The value is relative to the element's upper left corner.
        drawingContext.DrawText(formattedText, new Point());
    }

使用强制时呈现自定义控件失败

寻找更好的答案,但这有效。

将富文本定义更改为:

 public static readonly DependencyProperty RichTextProperty =
        DependencyProperty.Register(
        "RichText",                                 
        typeof(Paragraph),                          
        typeof(InkTextBlock),                       
        new FrameworkPropertyMetadata(
            null,                                               
            FrameworkPropertyMetadataOptions.AffectsRender,     
            OnRichTextPropertyChanged                           
            )                            
        );