xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.87k stars 871 forks source link

Enable Binding on Document in RichTextBox through other DependencyProperty #1384

Open RCP161 opened 5 years ago

RCP161 commented 5 years ago

Would it not be helpful to enable binding on Document? I did it in this way and it seems to work.

public class CustomRichtextBox : System.Windows.Controls.RichTextBox
{
    public static readonly DependencyProperty DocumentProperty = DependencyProperty.Register(nameof(FlowDocument), typeof(FlowDocument), typeof(CustomRichtextBox),new PropertyMetadata(OnDocumentChanged));

    public FlowDocument FlowDocument
    {
        get { return (FlowDocument)GetValue(DocumentProperty); }
        set { SetValue(DocumentProperty, value); }
    }

    private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = (CustomRichtextBox)d;
        if (e.NewValue == null || !(e.NewValue is FlowDocument))
            control.Document = new FlowDocument();
        else
            control.Document = (FlowDocument)e.NewValue;
    }
}
nonsensesoftware commented 5 years ago

Sure, that's something that you can do.

However, that really doesn't conform to the MVVM pattern as your view models should not be concerned with anything related to view. As an example, your view model should not care about alignment, style, content, and child content. Therefore, I don't believe it belongs in the main AvalonDock codebase, but is something you can do to extend RichTextBox if that is something you want to do.

RCP161 commented 5 years ago

Yeah, it was only an idea, in result, that the property already exists.

My Problem was, that I had a plain Text, that I want to style. I dind't found a rtf StringBuilder or somesthing similar to use the Text property, but the binding of Document is also not possible.

Did I miss something?

nonsensesoftware commented 5 years ago

If you take a look at the documentation for RichTextBox, you will see that by default it uses the RtfFormatter. You can use a RichTextBoxFormatBar attached to the RichTextBox in order to style your plain text.

RCP161 commented 5 years ago

Ok, a little more in detail: I am reading dynamicly different files and want to display them as one text. I want to show the name of the files on the top of each section ant the text below. To sort this a little bit, I want to make this headers (filenames) bold. The best way for me seems to be a rtf, but I didn't have a rtf. So I created a document, but this wasn't bindable.

How would you bold the headers?

nonsensesoftware commented 5 years ago

I'm not going to give a specific answer, but there are plenty of solutions online for what you are looking for.

Take a look at the RTF Spec, specifically at the control words for font character formatting properties. You should be able to easily parse the headers, append the appropriate RTF control words to the beginning and end of the string you want to bold.

If you don't want to look up the control spec, perhaps you can use the sample application provided in the repository with the RichTextBox, make a string bold, and see how it returns the RTF formatted string.

Here's a sample: Text: RichTextBox RTF Output: {\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\b\ltrch RichTextBox}\li0\ri0\sa0\sb0\fi0\ql\par} } }