-
Notifications
You must be signed in to change notification settings - Fork 9
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
Avoid negative MaxWidth values #28
base: main
Are you sure you want to change the base?
Conversation
MeasureOverride can be called during rendering, even before the control is rendered. This happens for example when the ItemsSource is defined declaratively. At that time Column.ActualWidth is still zero, and element is given a negative MaxWidth value. This causes the app to crash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, @XamlBrewer! The if condition you added ensures that element.MaxWidth is only set to a positive value, which is great. However, I believe we can simplify this logic further by using the Math.Max function. This approach ensures element.MaxWidth is never negative and will be zero if the calculated width is negative, all in a more concise manner.
Instead of:
if (contentWidth > 0)
{
element.MaxWidth = contentWidth;
}
We can use:
element.MaxWidth = Math.Max(contentWidth, 0);
Using Math.Max eliminates the need for an additional else block, thereby reducing the lines of code and maintaining clarity.
What do you think?
Actually I prefer my proposed implementation: only assign MaxWidth when it's relevant, otherwise don't touch it and let the basic layout routine do its work. Programmatically setting MaxWidth to zero basically makes the column or the cell invisible, and that might not be what you want. At the end of the day, users probably prefer suboptimal column auto-widths over disappearing colums (with MaxWidth zero). Alternatively, you can indeed use Math.Max but then I would suggest to use another threshold value (something greater than zero) as a kind of 'Minimum MaxWidth' that would keep the column at least visible and selectable. |
Approved the PR because it resolves the negative width assignment exception. |
I agree that the fix doesn't solve UI issues, it only prevents a crash. |
TableViewCell MeasureOverride can be called by the platform before the control is rendered. This happens for example when the ItemsSource is defined declaratively and column calculations are started before the page is loaded. Your sample app doesn't experience this, since it waits for a Loaded event from its hosting grid.
When MeasureOverride is called on a tableview that is not yet rendered, then Column.ActualWidth is still zero, and the element is given a negative MaxWidth value. This causes the app to crash.