2018년 1월 31일 수요일

[WPF실습예제] InotifyPropertyChanged 인테페이스를 이용한 데이터 바인딩 예제

(C#,WPF XAML 데이터바인딩)InotifyPropertyChanged 인테페이스를 이용한 데이터 바인딩 예제_WPF데이터바인딩교육/WPF출강학원 

INotifyPropertyChanged 인테페이스를 
이용한 데이터 바인딩 예제
n  INotifyPropertyChanged 인터페이스는 속성 값이 변경되면 클라이언트에 알리는 데 사용된다. INotifyPropertyChanged 인터페이스에는 PropertyChanged라는 이벤트가 포함되어 있는데 ViewModel / Model 개체의 속성이 변경되면 PropertyChanged 이벤트를 발생시켜 WPF 바인딩 시스템(VIEW, XAML)에 새 값을 알려준다.

n  INotifyTest 라는 이름으로 WPF 프로젝트 생성
n  Person.cs
using System.ComponentModel;
namespace INotifyTest{
    public class Person : INotifyPropertyChanged    {
        private string name;

        // Declare the event 
        public event PropertyChangedEventHandler PropertyChanged;

        public Person() { }

        public Person(string value) { this.name = value; }
        //아래 PersonName 속성 값이 변경되면
        public string PersonName
        {
            get { return name; }
            set
            {
                name = value;
                // Call OnPropertyChanged whenever the property is updated 
                OnPropertyChanged("PersonName");
            }
        }
        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

n  MainWindow.xaml
<Window x:Class="INotifyTest.MainWindow"
        xmlns:local="clr-namespace:INotifyTest"
        xmlns:src="clr-namespace:INotifyTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <!--<SnippetBindingSource>-->
    <Window.Resources>
        <src:Person x:Key="myDataSource" PersonName="홍길동"/>
        <!--</SnippetInstantiation>-->
        <Style TargetType="{x:Type Label}">
            <Setter Property="DockPanel.Dock" Value="Top"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="DockPanel.Dock" Value="Top"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="DockPanel.Dock" Value="Top"/>
            <Setter Property="Padding" Value="3"/>
        </Style>
        <!--<Snippet2>-->
    </Window.Resources>
    <!--</Snippet2>-->
    <Border Margin="5" BorderBrush="Aqua" BorderThickness="1" Padding="8" CornerRadius="3">
        <DockPanel Width="200" Height="100" Margin="35">
            <!-- <Snippet1> -->
            <Label>Enter a Name:</Label>
            <TextBox>
                <TextBox.Text>
                    <Binding Source="{StaticResource myDataSource}" Path="PersonName" UpdateSourceTrigger="PropertyChanged"/>
                </TextBox.Text>
            </TextBox>
            <!-- </Snippet1> -->
            <!--</SnippetBindingSource>-->
            <Label>The name you entered:</Label>
            <!--<SnippetBDO1>-->
            <TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
            <!--</SnippetBDO1>-->
        </DockPanel>
    </Border>
    <!--<SnippetEndWindow>-->
</Window>



댓글 없음:

댓글 쓰기