ImageResizerUnitToStringConverter.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.Globalization; 8 using System.Windows; 9 using Microsoft.PowerToys.Settings.UI.Library; 10 using Microsoft.UI.Xaml.Data; 11 12 namespace Microsoft.PowerToys.Settings.UI.Converters; 13 14 public sealed partial class ImageResizerUnitToStringConverter : IValueConverter 15 { 16 // Maps each ResizeUnit value to its localized string. 17 private static readonly Dictionary<ResizeUnit, string> UnitToText = new() 18 { 19 { ResizeUnit.Centimeter, Helpers.ResourceLoaderInstance.ResourceLoader.GetString("ImageResizer_Unit_Centimeter") }, 20 { ResizeUnit.Inch, Helpers.ResourceLoaderInstance.ResourceLoader.GetString("ImageResizer_Unit_Inch") }, 21 { ResizeUnit.Percent, Helpers.ResourceLoaderInstance.ResourceLoader.GetString("ImageResizer_Unit_Percent") }, 22 { ResizeUnit.Pixel, Helpers.ResourceLoaderInstance.ResourceLoader.GetString("ImageResizer_Unit_Pixel") }, 23 }; 24 25 public object Convert(object value, Type targetType, object parameter, string language) 26 { 27 if (value is ResizeUnit unit && UnitToText.TryGetValue(unit, out string unitText)) 28 { 29 return parameter is string lowerParam && lowerParam == "ToLower" ? 30 unitText.ToLower(CultureInfo.CurrentCulture) : 31 unitText; 32 } 33 34 return DependencyProperty.UnsetValue; 35 } 36 37 public object ConvertBack(object value, Type targetType, object parameter, string language) 38 { 39 return value; 40 } 41 }