Binding error in WPF using MVVM

I created my own control TextEditorthat inherits from AvalonEdit. I did this to facilitate the use of MVVM and Caliburn Micro with this editor. [Cut down for display purposes] MvvTextEditorclass

public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
    public MvvmTextEditor()
    {
        TextArea.SelectionChanged += TextArea_SelectionChanged;
    }

    void TextArea_SelectionChanged(object sender, EventArgs e)
    {
        this.SelectionStart = SelectionStart;
        this.SelectionLength = SelectionLength;
    }

    public static readonly DependencyProperty SelectionLengthProperty =
         DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
         new PropertyMetadata((obj, args) =>
             {
                 MvvmTextEditor target = (MvvmTextEditor)obj;
                 target.SelectionLength = (int)args.NewValue;
             }));

    public new int SelectionLength
    {
        get { return base.SelectionLength; }
        set { SetValue(SelectionLengthProperty, value); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged([CallerMemberName] string caller = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
    }
}

Now, in the view that contains this control, I have the following XAML:

    <Controls:MvvmTextEditor 
        Caliburn:Message.Attach="[Event TextChanged] = [Action DocumentChanged()]"
        TextLocation="{Binding TextLocation, Mode=TwoWay}"
        SyntaxHighlighting="{Binding HighlightingDefinition}" 
        SelectionLength="{Binding SelectionLength, 
                                  Mode=TwoWay, 
                                  NotifyOnSourceUpdated=True, 
                                  NotifyOnTargetUpdated=True}" 
        Document="{Binding Document, Mode=TwoWay}"/>

SelectionLength ( SelectionStart, , ). - , View to my View Model . , SelectionLength ( get set, TextEditor) . SelectionLength = 50, ,

private int selectionLength;
public int SelectionLength
{
    get { return selectionLength; }
    set
    {
        if (selectionLength == value)
            return;
        selectionLength = value;
        Console.WriteLine(String.Format("Selection Length = {0}", selectionLength));
        NotifyOfPropertyChange(() => SelectionLength);
    }
}

SelectionLength = 50, DependencyProperty SelectionLengthProperty MvvmTextEditor, TwoWay , Snoop . , , , , .

- , , MvvmTextEditor, DP [ ]

.

+4
3

...

public static readonly DependencyProperty SelectionLengthProperty =
     DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
     new PropertyMetadata((obj, args) =>
         {
             MvvmTextEditor target = (MvvmTextEditor)obj;
             if (target.SelectionLength != (int)args.NewValue)
             {
                 target.SelectionLength = (int)args.NewValue;
                 target.Select(target.SelectionStart, (int)args.NewValue);
             }
         }));

public new int SelectionLength
{
    get { return base.SelectionLength; }
    //get { return (int)GetValue(SelectionLengthProperty); }
    set { SetValue(SelectionLengthProperty, value); }
}

. , - ...

0

, Getter Setter DependencyProperty - .NET Wrapper. Framework GetValue SetValue.

- PropertyChangedCallback DependencyProperty .

 public int SelectionLength
        {
            get { return (int)GetValue(SelectionLengthProperty); }
            set { SetValue(SelectionLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectionLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectionLengthProperty =
            DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor), new PropertyMetadata(0,SelectionLengthPropertyChanged));


        private static void SelectionLengthPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var textEditor = obj as MvvmTextEditor;

            textEditor.SelectionLength = e.NewValue;
        }
+1

, . SelectionLength , ( ), .

System.ComponentModel.DependencyPropertyDescriptor, SelectionLength, .

:

public class SomeBehavior
{
    public static readonly DependencyProperty IsEnabledProperty
        = DependencyProperty.RegisterAttached("IsEnabled",
        typeof(bool), typeof(SomeBehavior), new PropertyMetadata(OnIsEnabledChanged));

    public static void SetIsEnabled(DependencyObject dpo, bool value)
    {
        dpo.SetValue(IsEnabledProperty, value);
    }

    public static bool GetIsEnabled(DependencyObject dpo)
    {
        return (bool)dpo.GetValue(IsEnabledProperty);
    }

    private static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
    {
        var editor = dpo as TextEditor;
        if (editor == null)
            return;

        var dpDescriptor = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(TextEditor.SelectionLengthProperty,editor.GetType());
        dpDescriptor.AddValueChanged(editor, OnSelectionLengthChanged);
    }

    private static void OnSelectionLengthChanged(object sender, EventArgs e)
    {
        var editor = (TextEditor)sender;
        editor.Select(editor.SelectionStart, editor.SelectionLength);
    }
}

Xaml :

  <Controls:TextEditor Behaviors:SomeBehavior.IsEnabled="True">
    </Controls:TextEditor>
+1

Source: https://habr.com/ru/post/1541755/


All Articles