From 2b0b09cd1a29d97e9cf0e4bc859d050c474ae225 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Sat, 14 Mar 2026 19:27:32 +0800 Subject: [PATCH] Fix two way bind --- .../v2rayN.Desktop/Views/JsonEditor.axaml.cs | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/v2rayN/v2rayN.Desktop/Views/JsonEditor.axaml.cs b/v2rayN/v2rayN.Desktop/Views/JsonEditor.axaml.cs index f577c85e..98620126 100644 --- a/v2rayN/v2rayN.Desktop/Views/JsonEditor.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/JsonEditor.axaml.cs @@ -15,12 +15,37 @@ public partial class JsonEditor : UserControl private static readonly Lazy SHighlightingLight = new(() => BuildHighlighting(dark: false), isThreadSafe: true); + public static readonly StyledProperty TextProperty = + AvaloniaProperty.Register(nameof(Text), defaultValue: string.Empty); + + public string Text + { + get => GetValue(TextProperty); + set => SetValue(TextProperty, value); + } + public JsonEditor() { InitializeComponent(); var isDark = Application.Current?.ActualThemeVariant != ThemeVariant.Light; Editor.SyntaxHighlighting = isDark ? SHighlightingDark.Value : SHighlightingLight.Value; Editor.TextArea.TextView.Options.EnableHyperlinks = false; + + Editor.TextChanged += (_, _) => + { + if (Text != Editor.Text) + { + SetCurrentValue(TextProperty, Editor.Text); + } + }; + + this.GetObservable(TextProperty).Subscribe(text => + { + if (Editor.Text != text) + { + Editor.Text = text ?? string.Empty; + } + }); } private static IHighlightingDefinition BuildHighlighting(bool dark) @@ -52,12 +77,6 @@ public partial class JsonEditor : UserControl return HighlightingLoader.Load(reader, HighlightingManager.Instance); } - public string Text - { - get => Editor.Text; - set => Editor.Text = value; - } - private void FormatJson_Click(object? sender, RoutedEventArgs e) { try