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

Add StringFormat option to text cells. #313

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

grokys
Copy link
Member

@grokys grokys commented Nov 1, 2024

This is similar to #283, but I used a slightly difference approach as the feature in that PR caused problems when text cells were edited.

The differences are:

  • The formatting is done in the TDG model layer (TextCell) rather than in the view layer (TreeDataGridTextCell) - this allows us to keep the edited text and the underlying value separate. This prevents the exception when editing.
  • The value is now only written to the bound model when editing ends. This prevents exceptions while editing the value (previously worked around in fix the empty cell value edit crash #276
  • The default culture is now CurrentCulture rather than InvariantCuture. This aligns with standard Avalonia bindings

Copy link

@giard-alexandre giard-alexandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this! Just one small potential suggestion to make things easier for consumers. Not blocking at all 🤷

/// <summary>
/// Gets the format string to be used to format the cell value.
/// </summary>
string StringFormat { get; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this won't be possible since ITextCellOptions is not generic and passing in the type of the property for the column but I thought I'd bring it up just in case you see a way to make it happen!
It would be nice to decorate the property with [StringSyntax(StringSyntaxAttribute.NumericFormat)] or any of the other derivatives.

Again, I don't think this would work HERE but it would be cool to be able to specify the stringsyntax somehow.

Copy link
Member Author

@grokys grokys Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, couldn't the string format be anything at all though? I can imagine it being using for displaying arbitrary strings in the cells, e.g. "{0} apple(s)". The StringFormat in standard Bindings can certainly be used that way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! I was thinking of having it set according to the TModel type, but you're right that the possibilities are so varied, I don't think it's worth going through all this trouble.

/// <summary>
/// Gets or sets the format string for the cells in the column.
/// </summary>
public string StringFormat { get; set; } = "{0}";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw that we have the TModel here! Might be able to use StringSyntaxAttribute after all?

@grokys grokys marked this pull request as ready for review November 5, 2024 13:23
@grokys grokys merged commit d4a77ff into master Nov 5, 2024
3 checks passed
@grokys grokys deleted the feature/textcell-stringformat branch November 5, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants