Making AvalonEdit MVVM compatible

前端 未结 3 880
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 05:09

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 AvalonEdi

3条回答
  •  余生分开走
    2020-12-13 05:29

    Not sure if this fits your needs, but I found a way to access all the "important" components of the TextEditor on a ViewModel while having it displayed on a View, still exploring the possibilities though.

    What I did was instead of instantiating the TextEditor on the View and then binding the many properties that I will need, I created a Content Control and bound its content to a TextEditor instance that I create in the ViewModel.

    View:

    
    

    ViewModel:

    using ICSharpCode.AvalonEdit;
    using ICSharpCode.AvalonEdit.Document;
    using ICSharpCode.AvalonEdit.Highlighting;
    // ...
    private TextEditor m_AvalonEditor = new TextEditor();
    public TextEditor AvalonEditor => m_AvalonEditor;
    

    Test code in the ViewModel (works!)

    // tests with the main component
    m_AvalonEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML");
    m_AvalonEditor.ShowLineNumbers = true;
    m_AvalonEditor.Load(@"C:\testfile.xml");
    
    // test with Options
    m_AvalonEditor.Options.HighlightCurrentLine = true;
    
    // test with Text Area
    m_AvalonEditor.TextArea.Opacity = 0.5;
    
    // test with Document
    m_AvalonEditor.Document.Text += "bla";
    

    At the moment I am still deciding exactly what I need my application to configure/do with the textEditor but from these tests it seems I can change any property from it while keeping a MVVM approach.

提交回复
热议问题