2019년 1월 26일 토요일

WPF 데이터바인딩 심플예제, INotifyPropertyChanged 인터페이스를 이용한 데이터 바인딩 예제,PropertyChanged 이벤트

WPF 데이터바인딩 심플예제, INotifyPropertyChanged 인터페이스를 이용한 데이터 바인딩 예제,PropertyChanged 이벤트
n INotifyPropertyChanged 인터페이스는 속성 값이 변경되면 클라이언트에 알리는 데 사용된다. INotifyPropertyChanged 인터페이스에는 PropertyChanged라는 이벤트가 포함되어 있는데 ViewModel / Model 개체의 속성이 변경되면 PropertyChanged 이벤트를 발생시켜 WPF 바인딩 시스템에 새 값을 알려준다.
n 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>
#INotifyPropertyChanged, #PropertyChanged, #데이터바인딩#WPF데이터바인딩#DataBinding#WPF#WPF강좌#WPF교육#WPF강의#시샵#닷넷#Csharp#XAML,

댓글 없음:

댓글 쓰기

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...