■ MultiBinding 클래스의 Converter 속성을 사용하는 방법을 보여준다.
▶ Employee.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 |
namespace TestProject { /// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 16세 이상 - IsOver16 /// <summary> /// 16세 이상 /// </summary> public bool IsOver16 { get; set; } #endregion #region 테스트 통과 여부 - HasPassedTest /// <summary> /// 테스트 통과 여부 /// </summary> public bool HasPassedTest { get; set; } #endregion #region 일시 중단 여부 - IsSuspended /// <summary> /// 일시 중단 여부 /// </summary> public bool IsSuspended { get; set; } #endregion } } |
▶ InverseBooleanConverter.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 |
using System.Globalization; namespace TestProject; /// <summary> /// 진리 값 반전 변환자 /// </summary> public class InverseBooleanConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { if(sourceValue is bool?) { bool? boolNullable = (bool?)sourceValue; return boolNullable.HasValue ? !boolNullable.Value : true; } if(sourceValue.GetType() == typeof(bool)) { return !((bool)sourceValue); } return true; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { if(sourceValue == null) { return true; } if(sourceValue is bool?) { bool? boolNullable = (bool?)sourceValue; return boolNullable.HasValue ? !boolNullable.Value : true; } if(sourceValue.GetType() == typeof(bool)) { return !((bool)sourceValue); } return true; } #endregion } |
▶ MultipleValueAllTrueConverter.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 |
using System.Globalization; namespace TestProject; /// <summary> /// 다중 값이 모두 참인 경우 변환자 /// </summary> public class MultipleValueAllTrueConverter : IMultiValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValueArray, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValueArray">소스 값 배열</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object[] sourceValueArray, Type targetType, object parameter, CultureInfo cultureInfo) { if(sourceValueArray == null || !targetType.IsAssignableFrom(typeof(bool))) { return false; } foreach(object sourceValue in sourceValueArray) { if(!(sourceValue is bool booleanValue)) { return false; } else if(!booleanValue) { return false; } } return true; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetTypeValue, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetTypeArray">타겟 타입 배열</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object[] ConvertBack(object sourceValue, Type[] targetTypeArray, object parameter, CultureInfo cultureInfo) { if(!(sourceValue is bool booleanValue) || targetTypeArray.Any(targetType => !targetType.IsAssignableFrom(typeof(bool)))) { return null; } if(booleanValue) { return targetTypeArray.Select(t => (object)true).ToArray(); } else { return null; } } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <StackLayout HorizontalOptions="Center" VerticalOptions="Center" Orientation="Horizontal"> <Label VerticalOptions="Center" Text="16세 이상, 테스트 통과, 일시 중단 없음" /> <CheckBox x:Name="checkBox" Margin="10,0,0,0" VerticalOptions="Center" /> </StackLayout> </ContentPage> |
▶ MainPage.xaml.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 |
using System.Collections.ObjectModel; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); BindingContext = new Employee { IsOver16 = true, HasPassedTest = true, IsSuspended = false }; this.checkBox.SetBinding ( CheckBox.IsCheckedProperty, new MultiBinding { Bindings = new Collection<BindingBase> { new Binding("IsOver16"), new Binding("HasPassedTest"), new Binding("IsSuspended", converter: new InverseBooleanConverter()) }, Converter = new MultipleValueAllTrueConverter() } ); } #endregion } |