DataGrid Column Proportional Widths with Decimal Separator is Dependent on CurrentCulture
#3072 aperta il 4 dic 2019
Metriche repository
- Star
- (5708 star)
- Metriche merge PR
- (Nessuna PR mergiata in 30 g)
Descrizione
Describe the bug
DataGrid column proportional widths with the decimal separator are dependent on CurrentCulture and can cause runtime exceptions to occur if the CurrentCulture is changed to a value that uses a different decimal separator than what is provided in the XAML.
Steps to Reproduce
Steps to reproduce the behavior:
- Add a DataGrid with at least one column with a proportional width including a decimal (e.g. ".3*").
- Change the CurrentCulture to a culture that uses a non-dot decimal separator (e.g. "fr-FR").
- Run the application.
- See runtime exception: "System.FormatException: 'Input string was not in a correct format.'"
Expected behavior
No runtime exception should be raised.
Additional thoughts
- The issue can be worked around by using whole numbers as proportional widths. This is my personal preference.
- Although the issue can be worked around, it appears that fractional values are legal. I cannot find documentation to prove or disprove this at this time.
- If the CurrentCulture is "fr-FR" (for example), ",3*" appears to be a legal width in XAML (further supporting the results found).
This issue can theoretically occur in at least the following scenarios:
- CurrentCulture is changed at runtime with a decimal separator in conflict with the decimal separator in the XAML column width.
- Source code is built on a machine with a CurrentCulture setting in conflict with the decimal separator in the XAML column width. This makes the source code less portable.
Environment
NuGet Package(s):
Microsoft.Toolkit.Uwp.UI.Controls.DataGrid
Package Version(s):
v6.0.0
Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] Insider Build (build number: )
- [x] November 2019 Update (18383)
App min and target version:
- [x] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] Insider Build (xxxxx)
(Appears to be a problem with other versions, but the app this was initially found and reproduced in targeted 16299 [min and target]).
Device form factor:
- [x] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT
Visual Studio
- [ ] 2017 (version: )
- [x] 2019 (version: 16.3.8)
- [ ] 2019 Preview (version: )
Additional context
Submitted a sample app reproducing the problem here.
The following generated code is where the exception surfaces (XamlTypeInfo.g.cs):
case 15: // Microsoft.Toolkit.Uwp.UI.Controls.DataGridLength
userType = new global::DataGrid_Culture_Issue.DataGrid_Culture_Issue_XamlTypeInfo.XamlUserType(this, typeName, type, GetXamlTypeByName("System.ValueType"));
userType.CreateFromStringMethod = x => (global::System.Object)global::Microsoft.Toolkit.Uwp.UI.Controls.DataGridLength.ConvertFromString(x);
userType.SetIsReturnTypeStub();
xamlType = userType;
break;
In the source code, it's easy to see where the problem is occurring. return ConvertFrom(null, value); is called, with the null parameter being the CultureInfo value passed. This eventually gets to the following line:
starWeight = Convert.ToDouble(stringValueWithoutSuffix, culture ?? CultureInfo.CurrentCulture);
This is where the CurrentCulture is used to convert the string value, potentially raising a System.FormatException.