첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.15MB

▶ 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
}

 

728x90

 

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요