diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs index 9cd1bc7844..8c4b5da848 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs @@ -8,10 +8,7 @@ public class FloatingHintClippingGridConverter : IMultiValueConverter { public object? Convert(object?[] values, Type targetType, object? parameter, CultureInfo culture) { - if (values is not [double actualWidth, double actualHeight, double floatingScale]) - { - return null; - } + if (values is not [double actualWidth, double actualHeight, double floatingScale]) return null; RectangleGeometry geometry = new(new Rect(new Point(0, 0), new Size(actualWidth, actualHeight * 2 * floatingScale))); geometry.Freeze(); diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs index 23f276561c..b5b90596a3 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs @@ -9,17 +9,15 @@ public class FloatingHintContainerMarginConverter : IMultiValueConverter public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture) { - if (values is [double scale, Thickness floatingMargin, double floatingScale]) + if (values is not [double scale, Thickness floatingMargin, double floatingScale]) return EmptyThickness; + + return floatingMargin with { - return floatingMargin with - { - Left = (floatingMargin.Left * scale) / floatingScale, - Top = (floatingMargin.Top * scale) / floatingScale, - Right = (floatingMargin.Right * scale) / floatingScale, - Bottom = (floatingMargin.Bottom * scale) / floatingScale - }; - } - return EmptyThickness; + Left = (floatingMargin.Left * scale) / floatingScale, + Top = (floatingMargin.Top * scale) / floatingScale, + Right = (floatingMargin.Right * scale) / floatingScale, + Bottom = (floatingMargin.Bottom * scale) / floatingScale + }; } public object[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs index b7af5d31da..4ad2d992a5 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs @@ -7,16 +7,28 @@ public class FloatingHintInitialHorizontalOffsetConverter : IMultiValueConverter { public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture) { - if (values is [double prefixWidth, Thickness prefixMargin, double suffixWidth, Thickness suffixMargin, PrefixSuffixVisibility prefixVisibility, PrefixSuffixVisibility suffixVisibility, PrefixSuffixHintBehavior prefixHintBehavior, PrefixSuffixHintBehavior suffixHintBehavior, HorizontalAlignment horizontalContentAlignment]) + if (values is not + [ + double prefixWidth, + Thickness prefixMargin, + double suffixWidth, + Thickness suffixMargin, + PrefixSuffixVisibility prefixVisibility, + PrefixSuffixVisibility suffixVisibility, + PrefixSuffixHintBehavior prefixHintBehavior, + PrefixSuffixHintBehavior suffixHintBehavior, + HorizontalAlignment horizontalContentAlignment + ]) { - return horizontalContentAlignment switch - { - HorizontalAlignment.Center => 0, - HorizontalAlignment.Right => GetRightOffset(), - _ => GetLeftOffset(), - }; + return 0; } - return 0; + + return horizontalContentAlignment switch + { + HorizontalAlignment.Center => 0, + HorizontalAlignment.Right => GetRightOffset(), + _ => GetLeftOffset(), + }; double GetLeftOffset() { diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs index 35995bf423..9d915768b7 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs @@ -1,4 +1,4 @@ -using System.Globalization; +using System.Globalization; using System.Windows.Data; namespace MaterialDesignThemes.Wpf.Converters; diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs index 73121365a6..1fbc786a07 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs @@ -8,10 +8,7 @@ public class FloatingHintScaleTransformConverter : IMultiValueConverter { public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture) { - if (values is not [double scale, double lower, double upper]) - { - return Transform.Identity; - } + if (values is not [double scale, double lower, double upper]) return Transform.Identity; double scalePercentage = upper + (lower - upper) * scale; return new ScaleTransform(scalePercentage, scalePercentage); diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs index 97c9d20353..ab2dd28dae 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs @@ -11,8 +11,13 @@ internal class FloatingHintTextBlockMarginConverter : IMultiValueConverter if (values is not [ FloatingHintHorizontalAlignment restingAlignmentOverride, - FloatingHintHorizontalAlignment floatingAlignment, HorizontalAlignment restingAlignment, - double desiredWidth, double availableWidth, double scale, double lower, double upper + FloatingHintHorizontalAlignment floatingAlignment, + HorizontalAlignment restingAlignment, + double desiredWidth, + double availableWidth, + double scale, + double lower, + double upper ]) { return Transform.Identity; diff --git a/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs index 06f0f16a72..c06f45b9c0 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs @@ -9,10 +9,10 @@ internal class GridLinesVisibilityBorderToThicknessConverter : IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - if (!(value is DataGridGridLinesVisibility visibility)) + if (value is not DataGridGridLinesVisibility visibility) return Binding.DoNothing; - var thickness = parameter as double? ?? GridLinesThickness; + double thickness = parameter as double? ?? GridLinesThickness; return visibility switch { diff --git a/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs index 00911a415a..dbd0ff3035 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs @@ -7,15 +7,11 @@ internal class HorizontalThicknessConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - if (value is Thickness thickness) - { - return new Thickness(thickness.Left, 0, thickness.Right, 0); - } - return Binding.DoNothing; + if (value is not Thickness thickness) return Binding.DoNothing; + + return new Thickness(thickness.Left, 0, thickness.Right, 0); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } + => throw new NotImplementedException(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs index 73b182a453..5ee66c335d 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs @@ -9,13 +9,11 @@ public class HsbLinearGradientConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - var v = (double)value; + if (value is not double hue) return Binding.DoNothing; - return new LinearGradientBrush(Colors.White, new Hsb(v, 1, 1).ToColor(), 0); + return new LinearGradientBrush(Colors.White, new Hsb(hue, 1, 1).ToColor(), 0); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } + => throw new NotImplementedException(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs index 028317c249..577ee60c4a 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs @@ -9,27 +9,25 @@ public class HsbToColorConverter : IValueConverter, IMultiValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - if (value is Hsb hsb) return new SolidColorBrush(hsb.ToColor()); - return Binding.DoNothing; + if (value is not Hsb hsb) return Binding.DoNothing; + + return new SolidColorBrush(hsb.ToColor()); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { - if (value is SolidColorBrush brush) return brush.Color.ToHsb(); - return Binding.DoNothing; + if (value is not SolidColorBrush brush) return Binding.DoNothing; + + return brush.Color.ToHsb(); } public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { - var h = (double)values[0]; - var s = (double)values[1]; - var b = (double)values[2]; + if (values is not [double hue, double saturation, double brightness]) return Binding.DoNothing; - return new SolidColorBrush(new Hsb(h, s, b).ToColor()); + return new SolidColorBrush(new Hsb(hue, saturation, brightness).ToColor()); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } + => throw new NotImplementedException(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs index 252aef4ed2..e99cd9403a 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs @@ -20,7 +20,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } + => throw new NotImplementedException(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs index f81bda4a59..7b5d41a1ec 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs @@ -28,12 +28,9 @@ public class ListViewGridViewConverter : IValueConverter /// public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { - if (value is ListView listView) - { - return listView.View != null ? ViewValue : DefaultValue; - } + if (value is not ListView listView) return value is ViewBase ? ViewValue : DefaultValue; - return value is ViewBase ? ViewValue : DefaultValue; + return listView.View != null ? ViewValue : DefaultValue; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs index f9f64591ed..80d88199a3 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs @@ -14,21 +14,15 @@ public sealed class MathConverter : IValueConverter { double value1 = System.Convert.ToDouble(value, CultureInfo.InvariantCulture); double value2 = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture); - switch (Operation) + return Operation switch { - case MathOperation.Add: - return value1 + value2 + Offset; - case MathOperation.Divide: - return value1 / value2 + Offset; - case MathOperation.Multiply: - return value1 * value2 + Offset; - case MathOperation.Subtract: - return value1 - value2 + Offset; - case MathOperation.Pow: - return Math.Pow(value1, value2) + Offset; - default: - return Binding.DoNothing; - } + MathOperation.Add => value1 + value2 + Offset, + MathOperation.Divide => value1 / value2 + Offset, + MathOperation.Multiply => value1 * value2 + Offset, + MathOperation.Subtract => value1 - value2 + Offset, + MathOperation.Pow => Math.Pow(value1, value2) + Offset, + _ => Binding.DoNothing, + }; } catch (FormatException) { diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs index 7bba007c20..a0380155c6 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs @@ -9,26 +9,17 @@ public sealed class MathMultipleConverter : IMultiValueConverter public object? Convert(object?[]? value, Type? targetType, object? parameter, CultureInfo? culture) { - if (value is null || value.Length < 2 || value[0] is null || value[1] is null) return Binding.DoNothing; + if (value is not [double value1, double value2]) return Binding.DoNothing; - if (!double.TryParse(value[0]!.ToString(), out double value1) || !double.TryParse(value[1]!.ToString(), out double value2)) - return 0; - - switch (Operation) + return Operation switch { - default: - // (case MathOperation.Add:) - return value1 + value2; - case MathOperation.Divide: - return value1 / value2; - case MathOperation.Multiply: - return value1 * value2; - case MathOperation.Subtract: - return value1 - value2; - case MathOperation.Pow: - return Math.Pow(value1, value2); - } - + MathOperation.Add => value1 + value2, + MathOperation.Divide => value1 / value2, + MathOperation.Multiply => value1 * value2, + MathOperation.Subtract => value1 - value2, + MathOperation.Pow => Math.Pow(value1, value2), + _ => Binding.DoNothing + }; } public object?[]? ConvertBack(object? value, Type[]? targetTypes, object? parameter, CultureInfo? culture) diff --git a/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs b/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs index c24b824138..21146323f9 100644 --- a/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs +++ b/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Windows.Data; using MaterialDesignThemes.Wpf.Converters; namespace MaterialDesignThemes.Wpf.Tests.Converters; @@ -16,4 +17,14 @@ public void EnumValues_AreAllHandled(MathOperation operation) Assert.True(converter.Convert(1.0, null, 1.0, CultureInfo.CurrentUICulture) is double); } + + [Fact] + public void NoneDoubleArguments_ShouldReturnDoNothing() + { + MathConverter converter = new(); + object? actual1 = converter.Convert("", null, 1.0, CultureInfo.CurrentUICulture); + Assert.Equal(Binding.DoNothing, actual1); + object? actual2 = converter.Convert(1.0, null, "", CultureInfo.CurrentUICulture); + Assert.Equal(Binding.DoNothing, actual2); + } }