MVVM: Access document from ViewModel

4 posts, 1 answers
  1. Philipp
    Philipp avatar
    12 posts
    Member since:
    Jan 2013

    Posted 08 Jan 2014 Link to this post

    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
  2. Yancho
    Admin
    Yancho avatar
    72 posts

    Posted 10 Jan 2014 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Philipp
    Philipp avatar
    12 posts
    Member since:
    Jan 2013

    Posted 13 Jan 2014 Link to this post

    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
  5. Answer
    Petya
    Admin
    Petya avatar
    975 posts

    Posted 16 Jan 2014 Link to this post

    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 >>
Back to Top