■ Binding 태그 확장의 TargetNullValue 속성을 사용해 NULL 바인딩시 대체 값을 설정하는 방법을 보여준다.
▶ Employee.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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
using System.ComponentModel; namespace TestProject; /// <summary> /// 직원 /// </summary> public class Employee : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 이벤트 - PropertyChanged /// <summary> /// 속성 변경시 이벤트 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID /// </summary> private int id; /// <summary> /// 성명 /// </summary> private string name; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get { return this.id; } set { if(this.id == value) { return; } this.id = value; FirePropertyChangedEvent(nameof(ID)); } } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get { return this.name; } set { if(this.name == value) { return; } this.name = value; FirePropertyChangedEvent(nameof(Name)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #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 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" x:Name="page" 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 ID="1" Name="{x:Null}" /> </ContentPage.BindingContext> <Grid HorizontalOptions="Center" VerticalOptions="Center"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Text="ID" /> <Label Grid.Row="0" Grid.Column="2" Text="{Binding ID}" /> <Label Grid.Row="2" Grid.Column="0" Text="성명" /> <Label Grid.Row="2" Grid.Column="2" Text="{Binding Name, TargetNullValue=무명인}" /> </Grid> </ContentPage> |