This is a migrated thread and some comments may be shown as answers.
Bind content to VM
2 Answers 33 Views
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 10 Jan 2020, 10:13 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 15 Jan 2020, 10:16 AM

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.

Regards,
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.
0
Richard
Top achievements
Rank 1
answered on 15 Jan 2020, 10:22 AM

Thanks Martin, 

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

Cheers,

Richard

Asked by
Richard
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Richard
Top achievements
Rank 1
Share this question
or