Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys committed Oct 31, 2024
1 parent 3d95836 commit 906620e
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 58 deletions.
2 changes: 1 addition & 1 deletion samples/TreeDataGridDemo/Models/Country.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class Country
public double? NetMigration { get; private set; }
//per 1000 births
public double? InfantMortality { get; private set; }
public int GDP { get; private set; }
public int GDP { get; set; }
public double? LiteracyPercent { get; private set; }
//per 1000
public double? Phones { get; private set; }
Expand Down
4 changes: 2 additions & 2 deletions samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public CountriesPageViewModel()
new TextColumn<Country, int>("Population", x => x.Population, new GridLength(3, GridUnitType.Star),
new TextColumnOptions<Country> { StringFormat = "{0:N}" }),
new TextColumn<Country, int>("Area", x => x.Area, new GridLength(3, GridUnitType.Star)),
new TextColumn<Country, int>("GDP", x => x.GDP, new GridLength(3, GridUnitType.Star), new()
new TextColumn<Country, int>("GDP", x => x.GDP, (o, v) => o.GDP = v, new GridLength(3, GridUnitType.Star), new()
{
TextAlignment = Avalonia.Media.TextAlignment.Right,
MaxWidth = new GridLength(150),
StringFormat = "{0:C}", // Format as Currency
FormatCultureInfo = CultureInfo.GetCultureInfo("en-US"), // Format this as USD
Culture = CultureInfo.GetCultureInfo("en-US"), // Format this as USD
}),
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ public interface ITextCell : ICell
/// </summary>
string? Text { get; set; }

/// <summary>
/// Format to use for the string
/// </summary>
string StringFormat { get; }

/// <summary>
/// Culture info used in conjunction with <see cref="StringFormat"/>
/// </summary>
CultureInfo FormatCultureInfo { get; }

/// <summary>
/// Gets the cell's text trimming mode.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace Avalonia.Controls.Models.TreeDataGrid
public interface ITextCellOptions : ICellOptions
{
/// <summary>
/// Format to use for the string
/// Gets the format string to be used to format the cell value.
/// </summary>
string StringFormat { get; }

/// <summary>
/// Culture info used in conjunction with <see cref="StringFormat"/>
/// Gets the culture to be used in conjunction with <see cref="StringFormat"/>.
/// </summary>
CultureInfo FormatCultureInfo { get; }
CultureInfo Culture { get; }

/// <summary>
/// Gets the text trimming mode for the cell.
Expand Down
14 changes: 10 additions & 4 deletions src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/TextCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ public TextCell(
public bool CanEdit => !IsReadOnly;
public BeginEditGestures EditGestures => _options?.BeginEditGestures ?? BeginEditGestures.Default;
public bool IsReadOnly { get; }
public string StringFormat => _options?.StringFormat ?? "{0}";
public CultureInfo FormatCultureInfo => _options?.FormatCultureInfo ?? CultureInfo.InvariantCulture;
public TextTrimming TextTrimming => _options?.TextTrimming ?? TextTrimming.None;
public TextWrapping TextWrapping => _options?.TextWrapping ?? TextWrapping.NoWrap;
public TextAlignment TextAlignment => _options?.TextAlignment ?? TextAlignment.Left;

public string? Text
{
get => _value?.ToString();
set{
get
{
if (_options?.StringFormat is { } format)
return string.Format(_options.Culture ?? CultureInfo.CurrentCulture, format, _value);
else
return _value?.ToString();
}

set
{
if (string.IsNullOrEmpty(value))
{
Value = default(T?);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TextColumnOptions<TModel> : ColumnOptions<TModel>, ITextCellOptions
/// <summary>
/// Culture info used in conjunction with <see cref="StringFormat"/>
/// </summary>
public CultureInfo FormatCultureInfo { get; set; } = CultureInfo.InvariantCulture;
public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture;

/// <summary>
/// Gets or sets the text trimming mode for the cells in the column.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
using System.ComponentModel;
using System.Globalization;

using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;

namespace Avalonia.Controls.Primitives
{
public class TreeDataGridTextCell : TreeDataGridCell
{
public static readonly DirectProperty<TreeDataGridTextCell, string> StringFormatProperty =
AvaloniaProperty.RegisterDirect<TreeDataGridTextCell, string>(
nameof(StringFormat),
o => o.StringFormat);

public static readonly DirectProperty<TreeDataGridTextCell, CultureInfo> FormatCultureInfoProperty =
AvaloniaProperty.RegisterDirect<TreeDataGridTextCell, CultureInfo>(
nameof(FormatCultureInfo),
o => o.FormatCultureInfo);

public static readonly DirectProperty<TreeDataGridTextCell, TextTrimming> TextTrimmingProperty =
AvaloniaProperty.RegisterDirect<TreeDataGridTextCell, TextTrimming>(
nameof(TextTrimming),
Expand All @@ -44,25 +31,11 @@ public class TreeDataGridTextCell : TreeDataGridCell
(o,v)=> o.TextAlignment = v);

private string? _value;
private string _stringFormat = "{0}";
private CultureInfo _formatCultureInfo = CultureInfo.InvariantCulture;
private TextBox? _edit;
private TextTrimming _textTrimming = TextTrimming.CharacterEllipsis;
private TextWrapping _textWrapping = TextWrapping.NoWrap;
private TextAlignment _textAlignment = TextAlignment.Left;

public string StringFormat
{
get => _stringFormat;
set => SetAndRaise(StringFormatProperty, ref _stringFormat, value);
}

public CultureInfo FormatCultureInfo
{
get => _formatCultureInfo;
set => SetAndRaise(FormatCultureInfoProperty, ref _formatCultureInfo, value);
}

public TextTrimming TextTrimming
{
get => _textTrimming;
Expand Down Expand Up @@ -90,17 +63,15 @@ public TextAlignment TextAlignment
get => _textAlignment;
set => SetAndRaise(TextAlignmentProperty, ref _textAlignment, value);
}

public override void Realize(
TreeDataGridElementFactory factory,
ITreeDataGridSelectionInteraction? selection,
ICell model,
int columnIndex,
int rowIndex)
{
var format = (model as ITextCell)?.StringFormat ?? "{0}";
var culture = (model as ITextCell)?.FormatCultureInfo ?? CultureInfo.InvariantCulture;

Value = string.Format(culture, format, model.Value);
Value = (model as ITextCell)?.Text;
TextTrimming = (model as ITextCell)?.TextTrimming ?? TextTrimming.CharacterEllipsis;
TextWrapping = (model as ITextCell)?.TextWrapping ?? TextWrapping.NoWrap;
TextAlignment = (model as ITextCell)?.TextAlignment ?? TextAlignment.Left;
Expand Down Expand Up @@ -131,11 +102,8 @@ protected override void OnModelPropertyChanged(object? sender, PropertyChangedEv
{
base.OnModelPropertyChanged(sender, e);

if (e.PropertyName != nameof(ITextCell.Value)) return;
var format = (Model as ITextCell)?.StringFormat ?? "{0}";
var culture = (Model as ITextCell)?.FormatCultureInfo ?? CultureInfo.InvariantCulture;

Value = string.Format(culture, format, Model?.Value);
if (e.PropertyName == nameof(ITextCell.Value))
Value = Model?.Value?.ToString();
}
}
}
41 changes: 41 additions & 0 deletions tests/Avalonia.Controls.TreeDataGrid.Tests/Models/TextCellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,46 @@ public void Modified_Value_Is_Not_Written_To_Binding_On_CancelEdit()
Assert.Equal("initial", target.Value);
Assert.Equal(new[] { "initial" }, result);
}

public class StringFormat
{
[AvaloniaFact(Timeout = 10000)]
public void Initial_Int_Value_Is_Formatted()
{
var binding = new BehaviorSubject<BindingValue<int>>(42);
var target = new TextCell<int>(binding, true, GetOptions());

Assert.Equal("foo42", target.Text);
Assert.Equal(42, target.Value);
}

[AvaloniaFact(Timeout = 10000)]
public void Int_Value_Is_Formatted_After_Editing()
{
var binding = new BehaviorSubject<BindingValue<int>>(42);
var target = new TextCell<int>(binding, false);
var result = new List<int>();

binding.Subscribe(x => result.Add(x.Value));

target.BeginEdit();
target.Text = "43";

Assert.Equal("43", target.Text);
Assert.Equal(43, target.Value);
Assert.Equal(new[] { 42 }, result);

target.EndEdit();

Assert.Equal("43.0", target.Text);
Assert.Equal(43, target.Value);
Assert.Equal(new[] { 42, 43 }, result);
}

private ITextCellOptions? GetOptions(string format = "foo{0}")
{
return new TextColumnOptions<int> { StringFormat = format };
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static TreeDataGridTextCell SetupCellForType<T>(T input, CultureInfo? cu
options.StringFormat = formatString;

if (cultureInfo is not null)
options.FormatCultureInfo = cultureInfo;
options.Culture = cultureInfo;
var model = new TextCell<T>(subject, true, options);
subject.OnNext(new BindingValue<T>(input));
cell.Realize(new TestElementFactory(), null, model, 0, 0);
Expand Down

0 comments on commit 906620e

Please sign in to comment.