■ Binding 태그 확장의 Converter 속성에서 정수↔진리 값 변환자를 사용하는 방법을 보여준다.
▶ IntegerToBooleanConverter.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 |
using System.Globalization; namespace TestProject; /// <summary> /// 정수↔진리 값 변환자 /// </summary> public class IntegerToBooleanConverter : 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) { return (int)sourceValue != 0; } #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 (bool)sourceValue ? 1 : 0; } #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 |
<?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" xmlns:local="clr-namespace:TestProject"> <ContentPage.Resources> <local:IntegerToBooleanConverter x:Key="IntegerToBooleanConverterKey" /> </ContentPage.Resources> <StackLayout HorizontalOptions="Center" VerticalOptions="Center"> <Entry x:Name="entry1" HorizontalOptions="Center" Placeholder="enter search term" Text="" /> <Button Margin="0,10,0,0" HorizontalOptions="Center" Text="Search" IsEnabled="{Binding Source={x:Reference entry1}, Path=Text.Length, Converter={StaticResource IntegerToBooleanConverterKey}}" /> <Entry x:Name="entry2" Margin="0,10,0,0" HorizontalOptions="Center" Placeholder="enter destination" Text="" /> <Button Margin="0,10,0,0" HorizontalOptions="Center" Text="Submit" IsEnabled="{Binding Source={x:Reference entry2}, Path=Text.Length, Converter={StaticResource IntegerToBooleanConverterKey}}" /> </StackLayout> </ContentPage> |