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

■ MultiBinding 클래스의 Converter 속성을 사용하는 방법을 보여준다.

TestProject.zip
0.15MB

▶ Employee.cs

namespace TestProject
{
    /// <summary>
    /// 직원
    /// </summary>
    public class Employee
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 16세 이상 - IsOver16

        /// <summary>
        /// 16세 이상
        /// </summary>
        public bool IsOver16 { get; set; }

        #endregion
        #region 테스트 통과 여부 - HasPassedTest

        /// <summary>
        /// 테스트 통과 여부
        /// </summary>
        public bool HasPassedTest { get; set; }

        #endregion
        #region 일시 중단 여부 - IsSuspended

        /// <summary>
        /// 일시 중단 여부
        /// </summary>
        public bool IsSuspended { get; set; }

        #endregion
    }
}

 

▶ InverseBooleanConverter.cs

using System.Globalization;

namespace TestProject;

/// <summary>
/// 진리 값 반전 변환자
/// </summary>
public class InverseBooleanConverter : 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
}

 

▶ MultipleValueAllTrueConverter.cs

using System.Globalization;

namespace TestProject;

/// <summary>
/// 다중 값이 모두 참인 경우 변환자
/// </summary>
public class MultipleValueAllTrueConverter : IMultiValueConverter
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 변환하기 - Convert(sourceValueArray, targetType, parameter, cultureInfo)

    /// <summary>
    /// 변환하기
    /// </summary>
    /// <param name="sourceValueArray">소스 값 배열</param>
    /// <param name="targetType">타겟 타입</param>
    /// <param name="parameter">매개 변수</param>
    /// <param name="cultureInfo">문화 정보</param>
    /// <returns>변환 값</returns>
    public object Convert(object[] sourceValueArray, Type targetType, object parameter, CultureInfo cultureInfo)
    {
        if(sourceValueArray == null || !targetType.IsAssignableFrom(typeof(bool)))
        {
            return false;
        }

        foreach(object sourceValue in sourceValueArray)
        {
            if(!(sourceValue is bool booleanValue))
            {
                return false;
            }
            else if(!booleanValue)
            {
                return false;
            }
        }

        return true;
    }

    #endregion
    #region 역변환하기 - ConvertBack(sourceValue, targetTypeValue, parameter, cultureInfo)

    /// <summary>
    /// 역변환하기
    /// </summary>
    /// <param name="sourceValue">소스 값</param>
    /// <param name="targetTypeArray">타겟 타입 배열</param>
    /// <param name="parameter">매개 변수</param>
    /// <param name="cultureInfo">문화 정보</param>
    /// <returns>역변환 값</returns>
    public object[] ConvertBack(object sourceValue, Type[] targetTypeArray, object parameter, CultureInfo cultureInfo)
    {
        if(!(sourceValue is bool booleanValue) || targetTypeArray.Any(targetType => !targetType.IsAssignableFrom(typeof(bool))))
        {
            return null;
        }

        if(booleanValue)
        {
            return targetTypeArray.Select(t => (object)true).ToArray();
        }
        else
        {
            return null;
        }
    }

    #endregion
}

 

▶ 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">
    <StackLayout
        HorizontalOptions="Center"
        VerticalOptions="Center"
        Orientation="Horizontal">
        <Label
            VerticalOptions="Center"
            Text="16세 이상, 테스트 통과, 일시 중단 없음" />
        <CheckBox x:Name="checkBox"
            Margin="10,0,0,0"
            VerticalOptions="Center" />
    </StackLayout>
</ContentPage>

 

▶ MainPage.xaml.cs

using System.Collections.ObjectModel;

namespace TestProject;

/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainPage()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainPage()
    {
        InitializeComponent();

        BindingContext = new Employee
        {
            IsOver16      = true,
            HasPassedTest = true,
            IsSuspended   = false
        };

        this.checkBox.SetBinding
        (
            CheckBox.IsCheckedProperty,
            new MultiBinding
            {
                Bindings = new Collection<BindingBase>
                {
                    new Binding("IsOver16"),
                    new Binding("HasPassedTest"),
                    new Binding("IsSuspended", converter: new InverseBooleanConverter())
                },
                Converter = new MultipleValueAllTrueConverter()
            }
        );
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요