728x90
반응형
728x170
■ x:DataType 속성을 DataTemplate 엘리먼트에서 사용하는 방법을 보여준다.
▶ NamedColor.cs
using System.Reflection;
namespace TestProject;
/// <summary>
/// 이름있는 색상
/// </summary>
public class NamedColor
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 이름있는 색상 배열
/// </summary>
private static NamedColor[] _namedColorArray;
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 명칭
/// </summary>
private string name;
/// <summary>
/// 색상
/// </summary>
private Color color;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 이름있는 색상 배열 - NamedColorArray
/// <summary>
/// 이름있는 색상 배열
/// </summary>
public static NamedColor[] NamedColorArray
{
get
{
return _namedColorArray;
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Public
#region 명칭 - Name
/// <summary>
/// 명칭
/// </summary>
public string Name
{
get
{
string name = this.name[0].ToString();
for(int i = 1; i < this.name.Length; i++)
{
name += (char.IsUpper(this.name[i]) ? " " : "") + this.name[i].ToString();
}
return name;
}
}
#endregion
#region 색상 - Color
/// <summary>
/// 색상
/// </summary>
public Color Color
{
get
{
return this.color;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Static
#region 생성자 - NamedColor()
/// <summary>
/// 생성자
/// </summary>
static NamedColor()
{
FieldInfo[] fieldInfoArray = typeof(Colors).GetFields(BindingFlags.Static | BindingFlags.Public);
_namedColorArray = new NamedColor[fieldInfoArray.Length];
for(int i = 0; i < fieldInfoArray.Length; i++)
{
_namedColorArray[i] = new NamedColor
(
fieldInfoArray[i].Name,
(Color)fieldInfoArray[i].GetValue(null)
);
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Private
#region 생성자 - NamedColor(name, color)
/// <summary>
/// 생성자
/// </summary>
/// <param name="name">명칭</param>
/// <param name="color">색상</param>
private NamedColor(string name, Color color)
{
this.name = name;
this.color = color;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 문자열 구하기 - ToString()
/// <summary>
/// 문자열 구하기
/// </summary>
/// <returns>문자열</returns>
public override string ToString()
{
return this.name;
}
#endregion
}
▶ ColorToStringConverter.cs
using System.Globalization;
namespace TestProject;
/// <summary>
/// 색상→문자열 변환자
/// </summary>
public class ColorToStringConverter : IValueConverter
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 변환하기 - (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)
{
Color color = sourceValue as Color;
if(color == null)
{
return null;
}
int alpha = (int)Math.Round(255 * color.Alpha);
int red = (int)Math.Round(255 * color.Red );
int green = (int)Math.Round(255 * color.Green);
int blue = (int)Math.Round(255 * color.Blue );
return $"{alpha:X2}:{red:X2}:{green:X2}:{blue:X2}";
}
#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>
/// <exception cref="NotImplementedException">미구현 예외</exception>
public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)
{
throw new NotImplementedException();
}
#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.Resources>
<x:Double x:Key="BoxSizeKey">50</x:Double>
<x:Int32 x:Key="RowHeightKey">60</x:Int32>
<local:ColorToStringConverter x:Key="ColorToStringConverterKey" />
</ContentPage.Resources>
<ListView
RowHeight="{StaticResource RowHeightKey}"
ItemsSource="{x:Static local:NamedColor.NamedColorArray}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:NamedColor">
<ViewCell>
<StackLayout
Orientation="Horizontal"
Padding="5,5,0,5"
Spacing="15">
<BoxView
WidthRequest="{StaticResource BoxSizeKey}"
HeightRequest="{StaticResource BoxSizeKey}"
Color="{Binding Color}" />
<StackLayout
VerticalOptions="Center"
Padding="5,0,0,0">
<Label
FontSize="Medium"
FontAttributes="Bold"
Text="{Binding Name}" />
<Label
Text="{Binding Color, Converter={StaticResource ColorToStringConverterKey}}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
728x90
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] PinchGestureRecognizer 클래스 : PinchUpdated 업데이트를 사용해 핀치 처리하기 (0) | 2022.03.18 |
---|---|
[C#/MAUI/.NET6] PanGestureRecognizer 클래스 : PanUpdated 이벤트를 사용해 패닝 처리하기 (0) | 2022.03.18 |
[C#/MAUI/.NET6] DeviceDisplay 클래스 : MainDisplayInfo 속성을 사용해 화면 크기 구하기 (0) | 2022.03.18 |
[C#/MAUI/.NET6] DragGestureRecognizer 클래스 : DragStarting 이벤트를 사용해 드래그 시작시 처리하기 (0) | 2022.03.17 |
[C#/MAUI/.NET6] x:DataType 속성 : 컴파일된 바인딩과 클래식 바인딩 함께 사용하기 (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] MultiBinding 엘리먼트 : Converter 속성에서 복수 값이 모두 참인 경우 변환자 사용하기 (0) | 2022.03.13 |
댓글을 달아 주세요