From cd252c0a0cc396cff5d0c0e5526609cf41f92b9a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 20 Nov 2024 14:45:59 +0100 Subject: [PATCH] Don't write value to model when change comes from model. If a changed value comes from the model, then we shouldn't be writing it back to the model. Usually this would be a no-op but when a `StringFormat` is applied it causes the formatted value to be sent back to the underlying data model, and if the `StringFormat` is not round-trippable causes an exception. --- .../Primitives/TreeDataGridTextCell.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridTextCell.cs b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridTextCell.cs index b56d09f5..510b35fe 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridTextCell.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridTextCell.cs @@ -33,6 +33,7 @@ public class TreeDataGridTextCell : TreeDataGridCell private string? _value; private TextBox? _edit; + private bool _modelValueChanging; private TextTrimming _textTrimming = TextTrimming.CharacterEllipsis; private TextWrapping _textWrapping = TextWrapping.NoWrap; private TextAlignment _textAlignment = TextAlignment.Left; @@ -54,7 +55,7 @@ public string? Value get => _value; set { - if (SetAndRaise(ValueProperty, ref _value, value) && Model is ITextCell cell) + if (SetAndRaise(ValueProperty, ref _value, value) && Model is ITextCell cell && !_modelValueChanging) cell.Text = _value; } } @@ -109,7 +110,17 @@ protected override void OnModelPropertyChanged(object? sender, PropertyChangedEv base.OnModelPropertyChanged(sender, e); if (e.PropertyName == nameof(ITextCell.Value)) - Value = Model?.Value?.ToString(); + { + try + { + _modelValueChanging = true; + Value = Model?.Value?.ToString(); + } + finally + { + _modelValueChanging = false; + } + } } } }