简体   繁体   English

向RichTextBox添加新行不起作用

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

I have a RichTextBox control in my WPF application. 我的WPF应用程序中有一个RichTextBox控件。 I'm binding the Text for RichTextBox to a property. 我将RichTextBox的文本绑定到一个属性。 I'm trying add a new line to the text, but it is not working. 我正在尝试在文本中添加新行,但无法正常工作。 I've tried to add "\\n", "Environment.NewLine". 我尝试添加“ \\ n”,“ Environment.NewLine”。 None of these work. 这些都不起作用。

This is what I have for the XAML: 这就是XAML的功能:

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

And this is what I have for the Text Property: 这就是我对Text属性的要求:

 private string emailBody;

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

Now, in my ViewModel Class,I'm trying to add a new line to the Property: 现在,在我的ViewModel类中,我试图向Property添加新行:

EmailBody += Environment.NewLine;

This is the Behavior Class for the HtmlRichTextBoxBehavior: 这是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;
        }

This isn't working. 这不起作用。 Any idea what I'm doing wrong here? 知道我在做什么错吗?

I Would try appending a new line text to the end of the value that is being set to emailBody instead of trying to assign it to EmailBody. 我将尝试在设置为emailBody的值的末尾追加新行文本,而不是尝试将其分配给EmailBody。

You should be able to use the same method your trying to use 您应该能够使用尝试使用的相同方法

value += System.Environment.NewLine;

I hope this helps 我希望这有帮助

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

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