INotifyPropertyChanged ?
속성 값이 변경되었음을 클라이언트에 알립니다.
예를 들어 UI 단에서 값이 변경되면 바인딩된 클래스의 값도 변경됩니다.
또는 INotifyPropertyChanged 를 클래스를 구현한 데이터를
A UI 와 B UI 에서 동시에 바인딩했다면 A UI 에서 값을 변경했다면 INotifyPropertyChanged 가 발생해서 자동으로 B UI 에서의 상태값도 변경됩니다.
예제
아래는 바인딩할 개체 입니다.
using System.ComponentModel; 사용하며 INotifyPropertyChanged 를 구현해주면 끗~
INotifyPropertyChanged 구현한 Person 클래스
using System.ComponentModel;
namespace SDKSample
{
// This class implements INotifyPropertyChanged
// to support one-way and two-way bindings
// (such that the UI element updates when the source
// has been changed dynamically)
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)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}WPF
<!--<SnippetInstantiation>-->
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SDKSample"
SizeToContent="WidthAndHeight"
Title="Simple Data Binding Sample">
<!--<SnippetBindingSource>-->
<Window.Resources>
<src:Person x:Key="myDataSource" PersonName="Joe"/>
<!--</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>
<!--</SnippetEndWindow>--> 'WPF' 카테고리의 다른 글
| [ListBox]초간단 리스트박스 바인딩 (1) | 2010.06.04 |
|---|---|
| [WPF] 기술/프로그램 샘플 (0) | 2010.06.04 |
| [Storyboard]애니메이션 시작 (0) | 2010.06.03 |
| WPF 기본컨트롤 (0) | 2010.06.01 |
| [WPF Toolkit] February 2010 Release 다운로드 (0) | 2010.06.01 |
| Silverlight Beta 4 Drag n Drop Feature(Picture album) (0) | 2010.05.15 |
| [WPF]초간단 윈도우즈 창 Drag(드래그) 하기 (0) | 2010.04.14 |
| [MSDN]BindingSource와 INotifyPropertyChanged 인터페이스를 사용하여 변경 내용 알림 발생 (0) | 2010.04.07 |
| 실버라이트로 windows mobile 7 프로그램 개발 (0) | 2010.03.17 |
| 태그삭제 Convert (0) | 2010.01.02 |