■ IValueConverter 인터페이스에서 열거형 값↔진리 값 변환자 사용하는 방법을 보여준다.
▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
xmlns:local="using:TestProject" <Page.Resources> <local:EnumerationToBooleanConverter x:Key="EnumerationToBooleanConverterKey" /> </Page.Resources> <RadioButton x:Uid="Settings_Theme_Light" GroupName="AppTheme" FontSize="15" IsChecked="{x:Bind ViewModel.ElementTheme, Converter={StaticResource EnumerationToBooleanConverterKey}, ConverterParameter=Light, Mode=OneWay}" Command="{x:Bind ViewModel.SwitchThemeCommand}"> <RadioButton.CommandParameter> <xaml:ElementTheme>Light</xaml:ElementTheme> </RadioButton.CommandParameter> </RadioButton> |
▶ EnumerationToBooleanConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Data; namespace TestProject; /// <summary> /// 열거형↔진리 값 변환자 /// </summary> public class EnumerationToBooleanConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - EnumerationToBooleanConverter() /// <summary> /// 생성자 /// </summary> public EnumerationToBooleanConverter() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, language) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="language">언어</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, string language) { if(parameter is string enumerationString) { if(!Enum.IsDefined(typeof(ElementTheme), sourceValue)) { throw new ArgumentException("Exception : EnumerationToBooleanConverter value must be enumeration."); } object enumerationValue = Enum.Parse(typeof(ElementTheme), enumerationString); return enumerationValue.Equals(sourceValue); } throw new ArgumentException("Exception : EnumerationToBooleanConverter parameter must be as enumeration name."); } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, language) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="language">언어</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, string language) { if(parameter is string enumerationString) { return Enum.Parse(typeof(ElementTheme), enumerationString); } throw new ArgumentException("Exception : EnumerationToBooleanConverter parameter must be as enumeration name."); } #endregion } |