728x90
반응형
728x170
■ ControlTemplate 엘리먼트를 사용해 템플리트에서 뷰 모델을 바인딩하는 방법을 보여준다.
▶ CardView.cs
namespace TestProject;
/// <summary>
/// 카드 뷰
/// </summary>
public partial class CardView : ContentView
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Bindable Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 테두리 색상 속성 - BorderColorProperty
/// <summary>
/// 테두리 색상 속성
/// </summary>
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create
(
nameof(BorderColor),
typeof(Color),
typeof(CardView),
Colors.DarkGray
);
#endregion
#region 카드 색상 속성 - CardColorProperty
/// <summary>
/// 카드 색상 속성
/// </summary>
public static readonly BindableProperty CardColorProperty = BindableProperty.Create
(
nameof(CardColor),
typeof(Color),
typeof(CardView),
Colors.White
);
#endregion
#region 아이콘 배경 색상 속성 - IconBackgroundColorProperty
/// <summary>
/// 아이콘 배경 색상 속성
/// </summary>
public static readonly BindableProperty IconBackgroundColorProperty = BindableProperty.Create
(
nameof(IconBackgroundColor),
typeof(Color),
typeof(CardView),
Colors.LightGray
);
#endregion
#region 아이콘 이미지 소스 속성 - IconImageSourceProperty
/// <summary>
/// 아이콘 이미지 소스 속성
/// </summary>
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create
(
nameof(IconImageSource),
typeof(ImageSource),
typeof(CardView),
default(ImageSource)
);
#endregion
#region 카드 제목 속성 - CardTitleProperty
/// <summary>
/// 카드 제목 속성
/// </summary>
public static readonly BindableProperty CardTitleProperty = BindableProperty.Create
(
nameof(CardTitle),
typeof(string),
typeof(CardView),
string.Empty
);
#endregion
#region 카드 설명 속성 - CardDescriptionProperty
/// <summary>
/// 카드 설명 속성
/// </summary>
public static readonly BindableProperty CardDescriptionProperty = BindableProperty.Create
(
nameof(CardDescription),
typeof(string),
typeof(CardView),
string.Empty
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 테두리 색상 - BorderColor
/// <summary>
/// 테두리 색상
/// </summary>
public Color BorderColor
{
get => (Color)GetValue(BorderColorProperty);
set => SetValue(BorderColorProperty, value);
}
#endregion
#region 카드 색상 - CardColor
/// <summary>
/// 카드 색상
/// </summary>
public Color CardColor
{
get => (Color)GetValue(CardColorProperty);
set => SetValue(CardColorProperty, value);
}
#endregion
#region 아이콘 배경 색상 - IconBackgroundColor
/// <summary>
/// 아이콘 배경 색상
/// </summary>
public Color IconBackgroundColor
{
get => (Color)GetValue(IconBackgroundColorProperty);
set => SetValue(IconBackgroundColorProperty, value);
}
#endregion
#region 아이콘 이미지 소스 - IconImageSource
/// <summary>
/// 아이콘 이미지 소스
/// </summary>
public ImageSource IconImageSource
{
get => (ImageSource)GetValue(IconImageSourceProperty);
set => SetValue(IconImageSourceProperty, value);
}
#endregion
#region 카드 제목 - CardTitle
/// <summary>
/// 카드 제목
/// </summary>
public string CardTitle
{
get => (string)GetValue(CardTitleProperty);
set => SetValue(CardTitleProperty, value);
}
#endregion
#region 카드 설명 - CardDescription
/// <summary>
/// 카드 설명
/// </summary>
public string CardDescription
{
get => (string)GetValue(CardDescriptionProperty);
set => SetValue(CardDescriptionProperty, value);
}
#endregion
}
▶ MainPageViewModel.cs
using System.Collections.ObjectModel;
using System.Windows.Input;
namespace TestProject;
/// <summary>
/// 메인 페이지 뷰 모델
/// </summary>
public class MainPageViewModel
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 사람 컬렉션 - PersonCollection
/// <summary>
/// 사람 컬렉션
/// </summary>
public ObservableCollection<PersonModel> PersonCollection { get; private set; }
#endregion
#region 사람 삭제 명령 - DeletePersonCommand
/// <summary>
/// 사람 삭제 명령
/// </summary>
public ICommand DeletePersonCommand { get; private set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainPageViewModel()
/// <summary>
/// 생성자
/// </summary>
public MainPageViewModel()
{
PersonCollection = new ObservableCollection<PersonModel>
{
new PersonModel
{
Name = "John Doe",
Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elit dolor, convallis non interdum."
},
new PersonModel
{
Name = "Jane Doe",
Description = "Phasellus eu convallis mi. In tempus augue eu dignissim fermentum. Morbi ut lacus vitae eros lacinia."
}
};
DeletePersonCommand = new Command<string>
(
(name) =>
{
PersonModel person = PersonCollection.Where(x => x.Name == name).FirstOrDefault();
if(person != null)
{
PersonCollection.Remove(person);
}
}
);
}
#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:MainPageViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
<ControlTemplate x:Key="CardViewControlTemplateKey">
<Frame
BindingContext="{Binding Source={RelativeSource TemplatedParent}}"
HorizontalOptions="Center"
VerticalOptions="Center"
CornerRadius="10"
BorderColor="{Binding BorderColor}"
BackgroundColor="{Binding CardColor}"
HasShadow="True"
Padding="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75" />
<RowDefinition Height="5" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
FontAttributes="Bold"
FontSize="18"
Text="{Binding CardTitle}" />
<BoxView Grid.Row="1"
HorizontalOptions="Fill"
HeightRequest="2"
BackgroundColor="{Binding BorderColor}" />
<Label Grid.Row="2"
HorizontalOptions="Fill"
VerticalOptions="Fill"
VerticalTextAlignment="Start"
Text="{Binding CardDescription}" />
<Button
HorizontalOptions="End"
Margin="0,10,0,10"
Text="삭제"
CommandParameter="{Binding CardTitle}"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainPageViewModel}}, Path=DeletePersonCommand}" />
</Grid>
</Frame>
</ControlTemplate>
<DataTemplate x:Key="PersonDataTemplateKey">
<local:CardView
ControlTemplate="{StaticResource CardViewControlTemplateKey}"
BorderColor="DarkGray"
CardTitle="{Binding Name}"
CardDescription="{Binding Description}" />
</DataTemplate>
</ContentPage.Resources>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center"
Margin="10"
Spacing="10"
BindableLayout.ItemTemplate="{StaticResource PersonDataTemplateKey}"
BindableLayout.ItemsSource="{Binding PersonCollection}" />
</ContentPage>
728x90
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] DataTemplateSelector 클래스 : 데이터 템플리트 선택자 사용하기 (0) | 2022.06.23 |
---|---|
[C#/MAUI/.NET6] DataTemplate 엘리먼트 : 리소스를 데이터 템플리트 사용하기 (0) | 2022.06.23 |
[C#/MAUI/.NET6] DataTemplate 엘리먼트 : 타입을 갖는 데이터 템플리트 사용하기 (0) | 2022.06.23 |
[C#/MAUI/.NET6] DataTemplate 엘리먼트 : 인라인 데이터 템플리트 사용하기 (0) | 2022.06.23 |
[C#/MAUI/.NET6] RelativeSource 태그 확장 : 바인딩 뷰 모델 참조하기 (0) | 2022.06.23 |
[C#/MAUI/.NET6] TemplatedView 클래스 : GetTemplateChild 메소드를 사용해 템플리트 내 객체 구하기 (0) | 2022.06.23 |
[C#/MAUI/.NET6] 프로젝트 파일에 스플래시 화면 추가하기 (0) | 2022.06.22 |
[C#/MAUI/.NET6] ContentPresenter 엘리먼트 사용하기 (0) | 2022.06.22 |
[C#/MAUI/.NET6] Style 엘리먼트 : TargetTye 속성을 사용해 컨트롤 템플리트 설정하기 (0) | 2022.06.22 |
[C#/MAUI/.NET6] ControlTemplate 엘리먼트 : ContentView 엘리먼트 정의하기 (커스텀 컨트롤 만들기) (0) | 2022.06.22 |
댓글을 달아 주세요