728x90
반응형
728x170
■ Binding 엘리먼트의 Converter 속성에서 진리 값↔객체 변환자를 사용하는 방법을 보여준다.
▶ BooleanToObjectConverter.cs
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
<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>
728x90
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] RelativeSource 태그 확장 : Mode 속성을 사용해 자기 자신 참조하기 (0) | 2022.06.17 |
---|---|
[C#/MAUI/.NET6] RelativeSource 태그 확장 : Mode 속성을 사용해 자기 자신 참조하기 (0) | 2022.06.17 |
[C#/MAUI/.NET6] IValueConverter 인터페이스 : 진리 값 반전 변환자 사용하기 (0) | 2022.06.17 |
[C#/MAUI/.NET6] IValueConverter 인터페이스 : 단정도 실수↔정수 변환자 사용하기 (0) | 2022.06.16 |
[C#/MAUI/.NET6] IValueConverter 인터페이스 : 진리 값↔객체 변환자 사용하기 (0) | 2022.06.16 |
[C#/MAUI/.NET6] Binding 태그 확장 : StringFormat 속성에서 중괄호({ }) 사용하기 (0) | 2022.06.16 |
[C#/MAUI/.NET6] Binding 태그 확장 : StringFormat 속성에서 이중 인용 부호(") 사용하기 (0) | 2022.06.16 |
[C#/MAUI/.NET6] Binding 태그 확장 : StringFormat 속성 사용하기 (0) | 2022.06.16 |
[C#/MAUI/.NET6] 프로젝트 파일에 폰트 파일 추가하기 (0) | 2022.06.15 |
[C#/MAUI/.NET6] SemanticProperties 엘리먼트 : HeadingLevel 첨부 속성 사용하기 (0) | 2022.06.15 |
댓글을 달아 주세요