첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

TestProject.zip
0.15MB

▶ InverseBooleanValueConverter.cs

using System.Globalization;

namespace TestProject;

/// <summary>
/// 불린 값 반전 변환자
/// </summary>
public class InverseBooleanValueConverter : 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
}

 

728x90

 

▶ MainPage.xaml

<?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:InverseBooleanValueConverter x:Key="InverseBooleanValueConverterKey" />
        <Style TargetType="BoxView">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup>
                        <VisualState x:Name="Checked">
                            <VisualState.StateTriggers>
                                <StateTrigger IsActive="{Binding Source={x:Reference switch}, Path=IsToggled}" />
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Property="Color" Value="Orange" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Unchecked">
                            <VisualState.StateTriggers>
                                <StateTrigger IsActive="{Binding Source={x:Reference switch}, Path=IsToggled, Converter={StaticResource InverseBooleanValueConverterKey}}" />
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Property="Color" Value="Yellow" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    <StackLayout
        HorizontalOptions="Center"
        VerticalOptions="Center">
        <Switch x:Name="switch"
            HorizontalOptions="Center" />
        <BoxView
            HorizontalOptions="Center"
            Margin="0,10,0,0"
            WidthRequest="300"
            HeightRequest="300" />
    </StackLayout>
</ContentPage>
728x90
그리드형(광고전용)
Posted by icodebroker
,