SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

MVVM and AvalonEdit

Last post 12-22-2014 8:31 PM by sahana. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-27-2010 8:56 PM

    • saner
    • Top 500 Contributor
    • Joined on 09-03-2009
    • Posts 13

    MVVM and AvalonEdit

    Hello,

    I bumped into a problem while trying to write application that uses MVVM pattern and AvalonEdit. I think that it is hard or even impossible to write that kind of application at the moment because you can only bind Document property of TextEditor, other important information about current state of this control can't be saved.

    In my view I've got TextEditor which Document field is bound to Document property of ViewModel. There is ObservableCollection of ViewModels which is bound to TabControl's data source. If you change tab, TabControl reuse View. It does so by binding appropriate fields of View to ViewModel. At the moment I can only bind Document property so I lose information about current caret position, current selection and probably TextAnchors (I didn't test that one).

     

    There are a few solutions which can be applied:

    • placing TextEditor control in model
    • subclassing TabControl so it won't reuse View

    Are there any plans of making AvalonEdit more MVVM friendly?

  • 07-27-2010 9:14 PM In reply to

    Re: MVVM and AvalonEdit

    No, there are no plans to make AvalonEdit more MVVM friendly.

    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.

  • 07-28-2010 3:38 PM In reply to

    • saner
    • Top 500 Contributor
    • Joined on 09-03-2009
    • Posts 13

    Re: MVVM and AvalonEdit

    Thanks for suggestion,

    What do you think what information should be available via dependency properties so that the current state of TextEditor can be saved?

  • 12-22-2014 8:31 PM In reply to

    • sahana
    • Not Ranked
    • Joined on 07-22-2014
    • Posts 5

    Re: MVVM and AvalonEdit

    I have AvalonEdit which is MVVM compatible, the code is as follows; However I have difficulty with other parts of my code. 

    namespace PatternEditor_MVVM.Model

    {

     

        public class MvvmTextEditor : ICSharpCode.AvalonEdit.TextEditor, INotifyPropertyChanged

        {

            /// <summary>

            /// A bindable Text property

            /// </summary>

            public new string Text

            {

                get { return base.Text; }

                set { base.Text = value; }

            }

     

            /// <summary>

            /// The bindable text property dependency property

            /// </summary>

            public static readonly DependencyProperty TextProperty =

                DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor), new PropertyMetadata((obj, args) =>

                {

                    var target = (MvvmTextEditor)obj;

                    target.Text = (string)args.NewValue;

                }));

     

            protected override void OnTextChanged(EventArgs e)

            {

                RaisePropertyChanged("Text");

                base.OnTextChanged(e);

            }

     

            /// <summary>

            /// Raises a property changed event

            /// </summary>

            /// <param name="property">The name of the property that updates</param>

            public void RaisePropertyChanged(string property)

            {

                if (PropertyChanged != null)

                {

                    PropertyChanged(this, new PropertyChangedEventArgs(property));

                }

            }

     

            public event PropertyChangedEventHandler PropertyChanged;

        }

    }

    I have the View.xaml as follows:

    <UserControl x:Class="PatternEditor_MVVM.View.EditorFileView"

                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

                 xmlns:vw="clr-namespace:PatternEditor_MVVM.View"  

                 xmlns:vm="clr-namespace:PatternEditor_MVVM.Model"             

                 mc:Ignorable="d" 

                 >

    <vm:MvvmTextEditor x:Name="EditorTextBlock" ShowLineNumbers="True" Text="{Binding Path=DataContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2"/>

    DataContent is a property in the ViewModel code is pasted below.When I change anything in the texteditor the setter of the DataContent in the below code is never invoked. However when I change the text box to a standard TextBox as the code below shows, the setter of the DataContent is invoked. What am I missing here? What should I change so that using MvvMTextEditor(aka MVVM compatible Avalon Editor) invokes the DataContent setter. Your help is greatly appreciated.

    <TextBox Name="EditorTextBlock" Text="{Binding Path=DataContent, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2"/>

    public abstract class EditorFileViewModel : ViewModelBase, IDataErrorInfo

        {

     public string DataContent

            {

                get { return _editorFile.DataContent; }

                set

                {

                    if (value == _editorFile.DataContent)

                        return;

     

                    _editorFile.DataContent = value;

                    _editorFile.ParserStatus = ParserStatusEnum.NotStarted;

                    _editorFile.PercentParsed = 0.0;

     

                    isFileDirty = true;

                    UpdateTabHeader();

     

                    base.OnPropertyChanged("DataContent");

                }

            }

    }

    Filed under:
Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.