From 04f5147cde23243e3faf7edf8ac23a80b44ffe93 Mon Sep 17 00:00:00 2001 From: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Date: Thu, 8 Feb 2024 15:06:27 +0100 Subject: [PATCH] [ImageResizer]Fix combo boxes bindings to hide height for percent(#31332) * [ImageResizer] Fix combo boxes bindings * Fix exception --- src/modules/imageresizer/ui/App.xaml | 1 + .../ui/Views/EnumToIntConverter.cs | 21 +++++++++++++++++++ .../ui/Views/EnumValueConverter.cs | 5 +++++ .../imageresizer/ui/Views/InputPage.xaml | 8 +++---- 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/modules/imageresizer/ui/Views/EnumToIntConverter.cs diff --git a/src/modules/imageresizer/ui/App.xaml b/src/modules/imageresizer/ui/App.xaml index 27b50ccee3..d177d900e6 100644 --- a/src/modules/imageresizer/ui/App.xaml +++ b/src/modules/imageresizer/ui/App.xaml @@ -18,6 +18,7 @@ + \ No newline at end of file diff --git a/src/modules/imageresizer/ui/Views/EnumToIntConverter.cs b/src/modules/imageresizer/ui/Views/EnumToIntConverter.cs new file mode 100644 index 0000000000..fcfd151c77 --- /dev/null +++ b/src/modules/imageresizer/ui/Views/EnumToIntConverter.cs @@ -0,0 +1,21 @@ +// Copyright (c) Brice Lambson +// The Brice Lambson licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ + +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace ImageResizer.Views +{ + [ValueConversion(typeof(Enum), typeof(int))] + internal class EnumToIntConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + => (int)value; + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + => targetType.GetEnumValues().GetValue((int)value); + } +} diff --git a/src/modules/imageresizer/ui/Views/EnumValueConverter.cs b/src/modules/imageresizer/ui/Views/EnumValueConverter.cs index 4eb0abdd25..efe421ff56 100644 --- a/src/modules/imageresizer/ui/Views/EnumValueConverter.cs +++ b/src/modules/imageresizer/ui/Views/EnumValueConverter.cs @@ -16,6 +16,11 @@ namespace ImageResizer.Views public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var type = value?.GetType(); + if (!type.IsEnum) + { + return value; + } + var builder = new StringBuilder(); builder diff --git a/src/modules/imageresizer/ui/Views/InputPage.xaml b/src/modules/imageresizer/ui/Views/InputPage.xaml index 0b90254fd0..b38879f0ba 100644 --- a/src/modules/imageresizer/ui/Views/InputPage.xaml +++ b/src/modules/imageresizer/ui/Views/InputPage.xaml @@ -179,9 +179,9 @@ HorizontalAlignment="Stretch" AutomationProperties.Name="{x:Static p:Resources.Resize_Type}" ItemsSource="{Binding ResizeFitValues, Mode=OneWay}" - SelectedIndex="0"> + SelectedIndex="{Binding ElementName=SizeComboBox, Path=SelectedValue.Fit, Mode=TwoWay, Converter={StaticResource EnumToIntConverter}}"> - + + SelectedIndex="{Binding ElementName=SizeComboBox, Path=SelectedValue.Unit, Mode=TwoWay, Converter={StaticResource EnumToIntConverter}}"> - +