[C#/MAUI/.NET6] MultiBinding 엘리먼트 : Converter 속성에서 복수 값이 모두 참인 경우 변환자 사용하기
C#/MAUI 2022. 3. 13. 00:49728x90
반응형
728x170
■ MultiBinding 엘리먼트의 Converter 속성에서 복수 값이 모두 참인 경우 변환자를 사용하는 방법을 보여준다.
▶ 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"
xmlns:local="clr-namespace:TestProject">
<ContentPage.BindingContext>
<local:Employee
IsOver16="True"
HasPassedTest="True"
IsSuspended="False" />
</ContentPage.BindingContext>
<ContentPage.Resources>
<local:MultipleValueAllTrueConverter x:Key="MultipleValueAllTrueConverterKey" />
<local:InverseBooleanConverter x:Key="InverseBooleanConverterKey" />
</ContentPage.Resources>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center"
Orientation="Horizontal">
<Label
VerticalOptions="Center"
Text="16세 이상, 테스트 통과, 일시 중단 없음" />
<CheckBox
Margin="10,0,0,0"
VerticalOptions="Center">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource MultipleValueAllTrueConverterKey}">
<Binding Path="IsOver16" />
<Binding Path="HasPassedTest" />
<Binding
Path="IsSuspended"
Converter="{StaticResource InverseBooleanConverterKey}" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackLayout>
</ContentPage>
728x90
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] x:DataType 속성 : DataTemplate 엘리먼트에서 사용하기 (0) | 2022.03.13 |
---|---|
[C#/MAUI/.NET6] x:DataType 속성 : 컴파일된 바인딩 사용하기 (0) | 2022.03.13 |
[C#/MAUI/.NET6] MultiBinding 클래스 : StringFormat 속성 사용하기 (0) | 2022.03.13 |
[C#/MAUI/.NET6] MultiBinding 엘리먼트 : StringFormat 속성 사용하기 (0) | 2022.03.13 |
[C#/MAUI/.NET6] MultiBinding 클래스 : Converter 속성 사용하기 (0) | 2022.03.13 |
[C#/MAUI/.NET6] IMultiValueConverter 인터페이스 : 복수 값이 모두 참인 경우 변환자 사용하기 (0) | 2022.03.13 |
[C#/MAUI/.NET6] Binding 태그 확장 : TargetNullValue 속성을 사용해 NULL 바인딩시 대체 값 설정하기 (0) | 2022.03.11 |
[C#/MAUI/.NET6] Binding 태그 확장 : FallbackValue 속성을 사용해 바인딩 오류시 대체 값 설정하기 (0) | 2022.03.11 |
[C#/MAUI/.NET6] RelativeSource 태그 확장 : Mode 속성을 사용해 조상 엘리먼트 참조하기 (0) | 2022.03.11 |
[C#/MAUI/.NET6] Binding 클래스 : ConverterParameter 속성 사용하기 (0) | 2022.03.10 |
댓글을 달아 주세요