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

TestProject.zip
0.15MB

▶ PersonModel.cs

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
    }
}

 

728x90

 

▶ PersonDataTemplateSelector.cs

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
}

 

300x250

 

▶ 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>
        <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>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요