WPF DataBinding, INotifyPropertyChanged 인터페이스를 이용한 데이터 바인딩 예제, WPF교육학원
http://ojc.asia/bbs/board.php?bo_table=WPF&wr_id=175
ojc.asia
https://www.youtube.com/watch?v=Orwyaq51MXQ&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=14&t=1182s

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

- INotifyTest 라는 이름으로 WPF 프로젝트 생성
- 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; }
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));
}
}
}
- MainWindow.xaml
<Window x:Class="INotifyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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>
#MVVM, #INotifyPropertyChanged , #WPFCommand, #WPFMVVM, #MVVM이란, #WPF강좌, #WPF교육, #WPF동영상, #WPF학원
MVVM, INotifyPropertyChanged , WPFCommand, WPFMVVM, MVVM이란, WPF강좌, WPF교육, WPF동영상, WPF학원
댓글 없음:
댓글 쓰기