Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TreeDataGridTextCell changing TextCell.Value #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public TextCell(
public string? Text
{
get => _value?.ToString();
set{
set
{
if (IsReadOnly) return;

if (string.IsNullOrEmpty(value))
{
Value = default(T?);
Expand Down
38 changes: 34 additions & 4 deletions tests/Avalonia.Controls.TreeDataGrid.Tests/Models/TextCellTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Data;
using Avalonia.Headless.XUnit;
using Avalonia.Media;
using Xunit;

namespace Avalonia.Controls.TreeDataGridTests.Models
Expand Down Expand Up @@ -95,5 +92,38 @@ public void Modified_Value_Is_Not_Written_To_Binding_On_CancelEdit()
Assert.Equal("initial", target.Value);
Assert.Equal(new[] { "initial" }, result);
}

[AvaloniaFact(Timeout = 10000)]
public void Setting_Text_Does_Not_Change_ReadOnly_Value()
{
var binding = new BehaviorSubject<BindingValue<CustomValueObject>>(value: new CustomValueObject(100));
var target = new TextCell<CustomValueObject>(binding, isReadOnly: true);

Assert.Equal(100, target.Value.Value);

// simulating TreeDataGridTextCell.OnModelPropertyChanged
target.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(ITextCell.Value))
target.Text = target.Value.ToString();
};

target.Value = new CustomValueObject(42);
Assert.Equal(42, target.Value.Value);
}

private readonly struct CustomValueObject
{
private readonly int _value;

public CustomValueObject(int value)
{
_value = value;
}

public int Value => _value;

public override string ToString() => _value.ToString(CultureInfo.InvariantCulture);
}
}
}