使用强制时无法呈现自定义控件?

时间:2014-12-14 20:45:06

标签: c# wpf xaml wpf-controls

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

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

感谢您的帮助。

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));

InkTextBlock控件:

 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());
    }

1 个答案:

答案 0 :(得分:0)

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

将RichText定义更改为:

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