Fix two way bind

This commit is contained in:
DHR60 2026-03-14 19:27:32 +08:00
parent 868efa6574
commit 2b0b09cd1a

View file

@ -15,12 +15,37 @@ public partial class JsonEditor : UserControl
private static readonly Lazy<IHighlightingDefinition> SHighlightingLight = private static readonly Lazy<IHighlightingDefinition> SHighlightingLight =
new(() => BuildHighlighting(dark: false), isThreadSafe: true); new(() => BuildHighlighting(dark: false), isThreadSafe: true);
public static readonly StyledProperty<string> TextProperty =
AvaloniaProperty.Register<JsonEditor, string>(nameof(Text), defaultValue: string.Empty);
public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public JsonEditor() public JsonEditor()
{ {
InitializeComponent(); InitializeComponent();
var isDark = Application.Current?.ActualThemeVariant != ThemeVariant.Light; var isDark = Application.Current?.ActualThemeVariant != ThemeVariant.Light;
Editor.SyntaxHighlighting = isDark ? SHighlightingDark.Value : SHighlightingLight.Value; Editor.SyntaxHighlighting = isDark ? SHighlightingDark.Value : SHighlightingLight.Value;
Editor.TextArea.TextView.Options.EnableHyperlinks = false; 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) private static IHighlightingDefinition BuildHighlighting(bool dark)
@ -52,12 +77,6 @@ public partial class JsonEditor : UserControl
return HighlightingLoader.Load(reader, HighlightingManager.Instance); return HighlightingLoader.Load(reader, HighlightingManager.Instance);
} }
public string Text
{
get => Editor.Text;
set => Editor.Text = value;
}
private void FormatJson_Click(object? sender, RoutedEventArgs e) private void FormatJson_Click(object? sender, RoutedEventArgs e)
{ {
try try