This is a migrated thread and some comments may be shown as answers.

MVVM: Access document from ViewModel

3 Answers 215 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Philipp
Top achievements
Rank 1
Philipp asked on 08 Jan 2014, 09:19 AM
Hello,

I have a view with x RadRichTextBoxes (x is dynamic) and want to access the content of each from my viewmodel. I know the DataProviders but I need more then binding just a string. E.g. I want to copy the selection from one RadRichTextBox to another one; including all the styles.
I tried a way via code-behind and it worked as expected:

            var editor1 = new RadDocumentEditor(radRichTextBox.Document);
            var editor2 = new RadDocumentEditor(radRichTextBox1.Document);

            editor1.Cut();
            editor2.Paste();

But I want to do all this in my viewmodel without code-behind.

Do you have any suggestions how I can do this?

Kind regards,
Philipp

3 Answers, 1 is accepted

Sort by
0
Missing User
answered on 10 Jan 2014, 05:11 PM
Hi Philipp,

Thank you for contacting us!

Unfortunately, you can’t copy тхе selection from one RadRichTextBox RadDocument instance to another because as in MS Word only the content of documents can be exported. Selecting functionality decorates the text, i.e. it is part of the document’s appearance in the control, not its content. 

The main reason why Data providers (and XamlDataProvider in particular) are created is to suit for any scenario requiring XAML - only binding. A clear example of this is using RIA Services and the MVVM model for creating applications. You can decide which rich text providers is the best to your binding data. This way you are able to copy the content of the document with all of its styles. Please take a look at this article regarding binding the provider to other UI Elements.

Additionally, if you choose to bind a data provider to a property in your view model, you can always create a new RadDocument instance from the property, modify the document using RadDocumentEditor and preserve it back in the same property all without using code-behind. For example, if you bind RadRichTextBox to a XamlData property using XamlDataProvider, here is how you can modify the document from your view model.
XamlFormatProvider provider = new XamlFormatProvider();
RadDocument document = provider.Import(XamlData);
RadDocumentEditor editor = new RadDocumentEditor(document);
//modify document using editor...
this.XamlData = provider.Export(editor.Document);

I hope this helps.
If you have any further questions, do not hesitate to contact us. 

Kind regards, Yancho
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Philipp
Top achievements
Rank 1
answered on 13 Jan 2014, 10:07 AM
Hi Yancho,

but I already copied the selected text with styles from one RadRichTextBox to another (as showed in my first post).

Thanks for the hint with the import function to create a RadDocumentEditor in my viewmodel but it would be nice to bind the selected text.
My scenario is to select text in document 1 and move it to document 2. Maybe this makes it easier to understand what I'm looking for.

King regards,
Philipp
0
Accepted
Petya
Telerik team
answered on 16 Jan 2014, 09:16 AM
Hello Philipp,

As previously mentioned, although RadDocument has a Selection property, the selection is considered part of the UI rather than the model of a document. For this reason preserving it without having access to the RadRichTextBox in which a document is shown is not possible. Unfortunately, there is not much else we can suggest in order for you to implement the desired scenario and I'd recommend using code-behind for the purposes of your application.

Let me know if you have further comments.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
RichTextBox
Asked by
Philipp
Top achievements
Rank 1
Answers by
Missing User
Philipp
Top achievements
Rank 1
Petya
Telerik team
Share this question
or