-
-
Notifications
You must be signed in to change notification settings - Fork 172
Create UniversalGridLengthConverter.cs #940
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,175 @@ | ||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||||||||||||||||||||||||
| using System.Globalization; | ||||||||||||||||||||||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||||||||||||||||||||||
| using System.Text; | ||||||||||||||||||||||||||||||||||||||||
| using System.Threading.Tasks; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| namespace UraniumUI.Material.Converters; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| public class UniversalGridLengthConverter : IValueConverter | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| culture ??= CultureInfo.CurrentCulture; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return value switch | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| null => GridLength.Auto, | ||||||||||||||||||||||||||||||||||||||||
| GridLength gl => gl, | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Строковые форматы | ||||||||||||||||||||||||||||||||||||||||
| string str => ParseAnyGridLengthFormat(str, culture), | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Числовые типы | ||||||||||||||||||||||||||||||||||||||||
| double d => new GridLength(d), | ||||||||||||||||||||||||||||||||||||||||
| int i => new GridLength(i), | ||||||||||||||||||||||||||||||||||||||||
| float f => new GridLength(f), | ||||||||||||||||||||||||||||||||||||||||
| decimal dec => new GridLength((double)dec), | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+29
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Из массива [value, type] | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+31
|
||||||||||||||||||||||||||||||||||||||||
| object[] arr when arr.Length == 2 && arr[0] is IConvertible val && arr[1] is GridUnitType type => | ||||||||||||||||||||||||||||||||||||||||
| new GridLength(System.Convert.ToDouble(val, culture), type), | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| _ => GridLength.Auto | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| culture ??= CultureInfo.CurrentCulture; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (value is not GridLength gridLength) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| // Возвращаем значение по умолчанию в зависимости от типа | ||||||||||||||||||||||||||||||||||||||||
| if (targetType == typeof(string)) | ||||||||||||||||||||||||||||||||||||||||
| return "Auto"; | ||||||||||||||||||||||||||||||||||||||||
| if (targetType == typeof(double)) | ||||||||||||||||||||||||||||||||||||||||
| return -1.0; | ||||||||||||||||||||||||||||||||||||||||
| if (targetType == typeof(GridLength)) | ||||||||||||||||||||||||||||||||||||||||
| return GridLength.Auto; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Преобразуем GridLength в нужный тип | ||||||||||||||||||||||||||||||||||||||||
| if (targetType == typeof(string)) | ||||||||||||||||||||||||||||||||||||||||
| return GridLengthToString(gridLength, culture); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (targetType == typeof(double) || targetType == typeof(float) || targetType == typeof(int)) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| // Для числовых типов возвращаем значение только если это Absolute | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+63
|
||||||||||||||||||||||||||||||||||||||||
| if (gridLength.GridUnitType == GridUnitType.Absolute) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| return targetType == typeof(int) ? (int)gridLength.Value : gridLength.Value; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| return targetType == typeof(int) ? -1 : -1.0; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| return targetType == typeof(int) ? -1 : -1.0; | |
| throw new NotSupportedException("Only GridLength values with GridUnitType.Absolute can be converted to numeric types."); |
Copilot
AI
Jan 26, 2026
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.
The ConvertBack method returns gridLength.Value as a fallback at the end, but this may not match the expected targetType. If targetType is a specific type that wasn't handled by the previous conditions, returning a double (gridLength.Value) may cause type mismatch issues. Consider making this more explicit or handling all expected target types.
| return gridLength.Value; // По умолчанию double | |
| // Если тип не поддерживается явно, возвращаем значение по умолчанию для targetType | |
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; |
Copilot
AI
Jan 26, 2026
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.
There's an extra blank line between methods. Looking at other converters in the codebase, there should be only one blank line between methods.
Copilot
AI
Jan 26, 2026
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.
This large block of commented-out code should be removed. If this is an alternative implementation that might be needed in the future, it should either be removed entirely (version control preserves it) or documented with a clear explanation of why it's kept. Keeping dead code reduces maintainability.
| /* | |
| public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
| { | |
| culture ??= CultureInfo.CurrentCulture; | |
| if (value is not GridLength gridLength) | |
| return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; | |
| return targetType.Name switch | |
| { | |
| nameof(Double) or "Double" => gridLength.Value, | |
| nameof(Single) or "Single" => (float)gridLength.Value, | |
| nameof(Int32) or "Int32" => (int)gridLength.Value, | |
| nameof(String) or "String" => GridLengthToString(gridLength, culture), | |
| _ => gridLength.Value // По умолчанию double | |
| }; | |
| } */ |
Copilot
AI
Jan 26, 2026
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.
The comma-to-dot replacement logic may not work correctly with cultures that use comma as a decimal separator. When you call TryParseGridLength with the normalized string, it still uses the culture parameter which may expect a comma. Consider using CultureInfo.InvariantCulture for the second parse attempt after normalizing to use a dot separator.
| if (TryParseGridLength(normalized, culture, out result)) | |
| if (TryParseGridLength(normalized, CultureInfo.InvariantCulture, out result)) |
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.
Several unused imports should be removed: System.Collections.Generic, System.Linq, System.Text, and System.Threading.Tasks are not used anywhere in this file. Only System, System.Globalization are actually needed.