【问题标题】:How to set a dependency property from InLine event of a textblock?如何从文本块的 InLine 事件中设置依赖属性?
【发布时间】:2015-01-25 02:48:07
【问题描述】:

我正在尝试从 CustomTextBlock 的 MouseEnter 的内联事件中设置依赖属性“WordPad”。但是这个错误结果:

非静态字段、方法或属性“WpfCustomControlLibrary.CustomTextBlock.WordPad.get”需要对象引用

我怎样才能做到这一点?

任何帮助将不胜感激。谢谢。

给定以下类:

 public class CustomTextBlock : TextBlock
{
      public string InLineText
    {
        get { return (string)GetValue(InLineTextProperty); }
        set { SetValue(InLineTextProperty, value); }
    }

     public static readonly DependencyProperty InLineTextProperty =
        DependencyProperty.Register("InLineText", typeof(string), typeof(CustomTextBlock),
            new FrameworkPropertyMetadata(string.Empty,
                FrameworkPropertyMetadataOptions.AffectsMeasure,
                (o, e) =>
                {
                    //PropertyChangedCallback

                    CustomTextBlock tb = (CustomTextBlock)o;
                    string text = (string)e.NewValue;

                    tb.Inlines.Clear();

                    if (String.IsNullOrEmpty(text))
                        return;

                    List<Inline> inlines = new List<Inline>();

                    string[] words = Regex.Split(text, @"(\s+)");

                    Inline inline = null;
                    foreach (string s in words)
                    {
                        Run run = new Run(s);
                        inline = run;
                        inline.MouseEnter += new System.Windows.Input.MouseEventHandler(inline_MouseEnter);
                        inline.MouseLeave += new System.Windows.Input.MouseEventHandler(inline_MouseLeave);
                        tb.Inlines.Add(inline);
                    }
                }));


     public WritingPad WordPad
    {
        get { return (WritingPad)GetValue(WordPadProperty); }
        set { SetValue(WordPadProperty, value); }
    }

     public static readonly DependencyProperty WordPadProperty =
        DependencyProperty.Register("WordPad", typeof(WritingPad), typeof(CustomTextBlock), new UIPropertyMetadata(null));


static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        Run Sender = sender as Run;
        TextPointer tp0 = Sender.ContentStart;
        TextPointer tp1 = Sender.ContentEnd;

        Rect StartRect = tp0.GetCharacterRect(LogicalDirection.Forward);
        Rect EndRect = tp1.GetCharacterRect(LogicalDirection.Backward);
        StartRect.Union(EndRect);

        WordPad = new WritingPad();   <--**THIS FAILS ????


    }

【问题讨论】:

    标签: wpf dependency-properties textblock


    【解决方案1】:

    inline_MouseEnterinline_MouseLeave 方法设为非静态并将它们附加到您的运行中,如下所示:

    inline.MouseEnter += tb.inline_MouseEnter;
    inline.MouseLeave += tb.inline_MouseLeave;
    

    更好的办法是让整个 PropertyChangedCallback 非静态,然后像这样编写依赖属性声明:

    public static readonly DependencyProperty InLineTextProperty =
        DependencyProperty.Register("InLineText", typeof(string), typeof(CustomTextBlock),
            new FrameworkPropertyMetadata(string.Empty,
                FrameworkPropertyMetadataOptions.AffectsMeasure,
                (o, e) => ((CustomTextBlock)o).InlineTextChanged((string)e.NewValue)));
    
    private void InlineTextChanged(string text)
    {
        Inlines.Clear();
    
        if (!string.IsNullOrEmpty(text))
        {
            foreach (string s in Regex.Split(text, @"(\s+)"))
            {
                var run = new Run(s);
                run.MouseEnter += inline_MouseEnter;
                run.MouseLeave += inline_MouseLeave;
                Inlines.Add(run);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我希望大师可以提供更好的解决方案。

      需要注意的是,Run 有一个“Parent”属性,它解析到这个 CustomTextBlock 实例。因此,这似乎可行,

        CustomTextBlock ctb = Sender.Parent as CustomTextBlock;
              ctb.WordPad = new WritingPad
              {
                  WordBounds = StartRect,
                  WritingPadMode = Enumerations.WritingPadModes.EditingByWord
              };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多