I’m trying to make Avalon MVVM compatible in my WPF application. From googling, I found out that AvalonEdit is not MVVM friendly and I need to export the state of AvalonEdit by making a class derived from TextEditor then adding the necessary dependency properties. I’m afraid that I’m quite lost in Herr Grunwald’s answer here:
If you really need to export the state of the editor using MVVM, then I suggest you create a class deriving from TextEditor which adds the necessary dependency properties and synchronizes them with the actual properties in AvalonEdit.
Does anyone have an example or have good suggestions on how to achieve this?
Herr Grunwald is talking about wrapping the
TextEditorproperties with dependency properties, so that you can bind to them. The basic idea is like this (using the CaretOffset property for example):Modified TextEditor class
Now that the
CaretOffsethas been wrapped in a DependencyProperty, you can bind it to a property, sayOffsetin your View Model. For illustration, bind aSlidercontrol’s value to the same View Model propertyOffset, and see that when you move the Slider, the Avalon editor’s cursor position gets updated:Test XAML
Test Code-behind
Test View Model