【问题标题】:WinRt: Binding a RTF String to a RichEditBoxWinRt:将 RTF 字符串绑定到 RichEditBox
【发布时间】:2014-12-20 08:45:03
【问题描述】:

搜索了很长时间以将一些 RTF 文本绑定到 Windows Store 应用程序上的 RichEditBox 控件。即使它应该在 TwoMay 绑定模式下运行。 ...

【问题讨论】:

    标签: c# windows-runtime windows-store-apps rtf uwp


    【解决方案1】:

    ...最后我找到了以下解决方案。我使用 DependencyProperty RtfText 从原始 RichEditBox 控件创建了一个继承控件。

    public class RichEditBoxExtended : RichEditBox
    {
        public static readonly DependencyProperty RtfTextProperty = 
            DependencyProperty.Register(
            "RtfText", typeof (string), typeof (RichEditBoxExtended),
            new PropertyMetadata(default(string), RtfTextPropertyChanged));
    
        private bool _lockChangeExecution;
    
        public RichEditBoxExtended()
        {
            TextChanged += RichEditBoxExtended_TextChanged;
        }
    
        public string RtfText
        {
            get { return (string) GetValue(RtfTextProperty); }
            set { SetValue(RtfTextProperty, value); }
        }
    
        private void RichEditBoxExtended_TextChanged(object sender, RoutedEventArgs e)
        {
            if (!_lockChangeExecution)
            {
                _lockChangeExecution = true;
                string text;
                Document.GetText(TextGetOptions.None, out text);
                if (string.IsNullOrWhiteSpace(text))
                {
                    RtfText = "";
                }
                else
                {
                    Document.GetText(TextGetOptions.FormatRtf, out text);
                    RtfText = text;
                }
                _lockChangeExecution = false;
            }
        }
    
        private static void RtfTextPropertyChanged(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var rtb = dependencyObject as RichEditBoxExtended;
            if (rtb == null) return;
            if (!rtb._lockChangeExecution)
            {
                rtb._lockChangeExecution = true;
                rtb.Document.SetText(TextSetOptions.FormatRtf, rtb.RtfText);
                rtb._lockChangeExecution = false;
            }
        }
    }
    

    此解决方案适用于我 - 或许也适用于其他人。 :-)

    已知问题: VirtualizingStackPanel.VirtualizationMode="Recycling" 中的奇怪行为

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 2015-09-27
      相关资源
      最近更新 更多