■ DataTemplateSelector 클래스에서 데이터 템플리트 선택자를 사용하는 방법을 보여준다.
▶ PersonModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace TestProject { /// <summary> /// 사람 모델 /// </summary> public class PersonModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - Name /// <summary> /// 이름 /// </summary> public string Name { get; set; } #endregion #region 나이 - Age /// <summary> /// 나이 /// </summary> public int Age { get; set; } #endregion #region 위치 - Location /// <summary> /// 위치 /// </summary> public string Location { get; set; } #endregion } } |
▶ PersonDataTemplateSelector.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
namespace TestProject; /// <summary> /// 사람 데이터 템플리트 선택자 /// </summary> public class PersonDataTemplateSelector : DataTemplateSelector { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 젊은 사람 템플리트 - YoungPersonTemplate /// <summary> /// 젊은 사람 템플리트 /// </summary> public DataTemplate YoungPersonTemplate { get; set; } #endregion #region 늙은 사람 템플리트 - OldPersonTemplate /// <summary> /// 늙은 사람 템플리트 /// </summary> public DataTemplate OldPersonTemplate { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 템플리트 선택시 처리하기 - OnSelectTemplate(item, container) /// <summary> /// 템플리트 선택시 처리하기 /// </summary> /// <param name="item">항목</param> /// <param name="container">컨테이너</param> /// <returns>선택 데이터 템플리트</returns> protected override DataTemplate OnSelectTemplate(object item, BindableObject container) { return ((PersonModel)item).Age < 35 ? YoungPersonTemplate : OldPersonTemplate; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?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> <DataTemplate x:Key="YoungPersonDataTemplateKey"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Column="0" FontAttributes="Bold" Text="{Binding Name}" /> <Label Grid.Column="1" TextColor="Blue" Text="{Binding Age}" /> <Label Grid.Column="2" HorizontalTextAlignment="End" Text="{Binding Location}" /> </Grid> </DataTemplate> <DataTemplate x:Key="OldPersonDataTemplateKey"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Column="0" FontAttributes="Bold" Text="{Binding Name}" /> <Label Grid.Column="1" TextColor="Red" Text="{Binding Age}" /> <Label Grid.Column="2" HorizontalTextAlignment="End" Text="{Binding Location}" /> </Grid> </DataTemplate> <local:PersonDataTemplateSelector x:Key="PersonDataTemplateSelectorKey" YoungPersonTemplate="{StaticResource YoungPersonDataTemplateKey}" OldPersonTemplate="{StaticResource OldPersonDataTemplateKey}" /> </ContentPage.Resources> <CollectionView ItemTemplate="{StaticResource PersonDataTemplateSelectorKey}" HorizontalOptions="Center" VerticalOptions="Center" Margin="10"> <CollectionView.ItemsSource> <x:Array Type="{x:Type local:PersonModel}"> <local:PersonModel Name="Steve" Age="21" Location="USA" /> <local:PersonModel Name="John" Age="37" Location="USA" /> <local:PersonModel Name="Tom" Age="42" Location="UK" /> <local:PersonModel Name="Lucas" Age="29" Location="Germany" /> <local:PersonModel Name="Tariq" Age="39" Location="UK" /> <local:PersonModel Name="Jane" Age="30" Location="USA" /> </x:Array> </CollectionView.ItemsSource> </CollectionView> </ContentPage> |