Bind content to VM

3 posts, 1 answers
  1. Richard
    Richard avatar
    97 posts
    Member since:
    Feb 2009

    Posted 10 Jan Link to this post

    Is it possible to have a two way binding of the text content in the editor to a string property in the View Model?
  2. Answer
    Martin Ivanov
    Martin Ivanov avatar
    2298 posts

    Posted 15 Jan Link to this post

    Hello Richard,

    The SyntaxEditor doesn't support direct binding to the internal text. This is because the content is not a plain text, but a more complex document model stored in the TextDocument class. To achieve your requirement you can store the TextDocument object in your view model and work with it when necessary.

    Or you can try to implement a custom synchronization between the text in your view model and the content in the TextDocument. For example, when the text in the view model changes, you can clear the document and insert the new text.

    syntaxEditor.Document.Delete(new Span(0, syntaxEditor.Document.CurrentSnapshot.Length));
    syntaxEditor.Document.Insert(0, text);

    When the text in the UI changes, you can use the TextContentChanged event of the TextDocument.

    private void TextDocument_TextContentChanged(object sender, TextContentChangedEventArgs e)
    	var newText = e.AfterChangeSnapshot.GetText();

    I hope this helps.

    Martin Ivanov
    Progress Telerik

    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. Richard
    Richard avatar
    97 posts
    Member since:
    Feb 2009

    Posted 15 Jan in reply to Martin Ivanov Link to this post

    Thanks Martin, 

    I thought that might be the case and I can make your suggestion work for me. 



Back to Top