alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Windows;
 
namespace OpenTapEditor.UI
{
    public class MvvmTextEditor : ICSharpCode.AvalonEdit.TextEditor
    {
        public static readonly DependencyProperty DocumentTextProperty =
            DependencyProperty.Register(
                "DocumentText",
                typeof(string),
                typeof(MvvmTextEditor),
                new FrameworkPropertyMetadata(
                    string.Empty,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    OnDocumentTextChanged));
 
        public string DocumentText
        {
            get { return (string)GetValue(DocumentTextProperty); }
            set { SetValue(DocumentTextProperty, value); }
        }
 
        private static void OnDocumentTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is MvvmTextEditor editor)
            {
                if (editor.Document != null && editor.Document.Text != (string)e.NewValue)
                {
                    editor.Document.Text = (string)e.NewValue ?? string.Empty;
                }
            }
        }
 
        protected override void OnTextChanged(EventArgs e)
        {
            SetCurrentValue(DocumentTextProperty, base.Text);
            base.OnTextChanged(e);
        }
    }
}