■ Binding 엘리먼트의 Converter 속성에서 진리 값↔객체 변환자를 사용하는 방법을 보여준다.
▶ BooleanToObjectConverter.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 |
using System.Globalization; namespace TestProject; /// <summary> /// 진리 값↔객체 변환자 /// </summary> /// <typeparam name="T">객체 타입</typeparam> public class BooleanToObjectConverter<T> : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 참 객체 - TrueObject /// <summary> /// 참 객체 /// </summary> public T TrueObject { get; set; } #endregion #region 거짓 객체 - FalseObject /// <summary> /// 거짓 객체 /// </summary> public T FalseObject { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// 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) { return (bool)sourceValue ? TrueObject : FalseObject; } #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) { return ((T)sourceValue).Equals(TrueObject); } #endregion } |
▶ MainPage.xaml
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 80 81 82 83 84 85 86 87 88 89 90 91 92 |
<ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestProject"> <ContentPage.Resources> <Style TargetType="Label"> <Setter Property="VerticalOptions" Value="Center" /> <Setter Property="FontSize" Value="18" /> </Style> <Style TargetType="Switch"> <Setter Property="VerticalOptions" Value="Center" /> </Style> </ContentPage.Resources> <StackLayout HorizontalOptions="Center" VerticalOptions="Center"> <StackLayout Orientation="Horizontal"> <Label Text="Subscribe?" /> <Switch x:Name="switch1" /> <Label> <Label.Text> <Binding Source="{x:Reference switch1}" Path="IsToggled"> <Binding.Converter> <local:BooleanToObjectConverter x:TypeArguments="x:String" TrueObject="Of course!" FalseObject="No way!" /> </Binding.Converter> </Binding> </Label.Text> </Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Text="Allow popups?" /> <Switch x:Name="switch2" /> <Label> <Label.Text> <Binding Source="{x:Reference switch2}" Path="IsToggled"> <Binding.Converter> <local:BooleanToObjectConverter x:TypeArguments="x:String" TrueObject="Yes" FalseObject="No" /> </Binding.Converter> </Binding> </Label.Text> <Label.TextColor> <Binding Source="{x:Reference switch2}" Path="IsToggled"> <Binding.Converter> <local:BooleanToObjectConverter x:TypeArguments="Color" TrueObject="Green" FalseObject="Red" /> </Binding.Converter> </Binding> </Label.TextColor> </Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Text="Learn more?" /> <Switch x:Name="switch3" /> <Label VerticalOptions="Center" FontSize="18"> <Label.Style> <Binding Source="{x:Reference switch3}" Path="IsToggled"> <Binding.Converter> <local:BooleanToObjectConverter x:TypeArguments="Style"> <local:BooleanToObjectConverter.TrueObject> <Style TargetType="Label"> <Setter Property="FontAttributes" Value="Italic, Bold" /> <Setter Property="TextColor" Value="Green" /> <Setter Property="Text" Value="Indubitably!" /> </Style> </local:BooleanToObjectConverter.TrueObject> <local:BooleanToObjectConverter.FalseObject> <Style TargetType="Label"> <Setter Property="FontAttributes" Value="None" /> <Setter Property="TextColor" Value="Red" /> <Setter Property="Text" Value="Maybe later" /> </Style> </local:BooleanToObjectConverter.FalseObject> </local:BooleanToObjectConverter> </Binding.Converter> </Binding> </Label.Style> </Label> </StackLayout> </StackLayout> </ContentPage> |