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

■ Binding 태그 확장의 TargetNullValue 속성을 사용해 NULL 바인딩시 대체 값을 설정하는 방법을 보여준다.

TestProject.zip
0.15MB

▶ Employee.cs

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

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

댓글을 달아 주세요