繁体   English   中英

向RichTextBox添加新行不起作用

[英]Adding a new line to RichTextBox is not working

我的WPF应用程序中有一个RichTextBox控件。 我将RichTextBox的文本绑定到一个属性。 我正在尝试在文本中添加新行,但无法正常工作。 我尝试添加“ \\ n”,“ Environment.NewLine”。 这些都不起作用。

这就是XAML的功能:

  <RichTextBox Name="EmailBody" resources:HtmlRichTextBoxBehavior.Text="{Binding EmailBody}" IsDocumentEnabled="True" AcceptsTab="True"  ScrollViewer.VerticalScrollBarVisibility="Auto" SpellCheck.IsEnabled="True"/>

这就是我对Text属性的要求:

 private string emailBody;

    public string EmailBody
    {
        get { return emailBody; }
        set
        {
            if (value != emailBody)
            {
                emailBody = value;
                OnPropertyChanged("EmailBody");
            }
        }
    }

现在,在我的ViewModel类中,我试图向Property添加新行:

EmailBody += Environment.NewLine;

这是HtmlRichTextBoxBehavior的行为类:

      public class HtmlRichTextBoxBehavior : ObservableObject
    {
        public static readonly DependencyProperty TextProperty =
   DependencyProperty.RegisterAttached("Text", typeof(string),
   typeof(HtmlRichTextBoxBehavior), new UIPropertyMetadata(null, OnValueChanged));

        public static string GetText(RichTextBox o) { return (string)o.GetValue(TextProperty); }

        public static void SetText(RichTextBox o, string value) { o.SetValue(TextProperty, value); }

        private static void OnValueChanged(DependencyObject dependencyObject,
          DependencyPropertyChangedEventArgs e)
        {
            var richTextBox = (RichTextBox)dependencyObject;
            var text = (e.NewValue ?? string.Empty).ToString();
            var xaml = HtmlToXamlConverter.ConvertHtmlToXaml(text, true);
            var flowDocument = XamlReader.Parse(xaml) as FlowDocument;
            HyperlinksSubscriptions(flowDocument);
            richTextBox.Document = flowDocument;
        }

        private static void HyperlinksSubscriptions(FlowDocument flowDocument)
        {
            if (flowDocument == null) return;
            GetVisualChildren(flowDocument).OfType<Hyperlink>().ToList()
                     .ForEach(i => i.RequestNavigate += HyperlinkNavigate);
        }

        private static IEnumerable<DependencyObject> GetVisualChildren(DependencyObject root)
        {
            foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
            {
                yield return child;
                foreach (var descendants in GetVisualChildren(child)) yield return descendants;
            }
        }

        private static void HyperlinkNavigate(object sender,
         System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

这不起作用。 知道我在做什么错吗?

我将尝试在设置为emailBody的值的末尾追加新行文本,而不是尝试将其分配给EmailBody。

您应该能够使用尝试使用的相同方法

value += System.Environment.NewLine;

我希望这有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM