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>-->  

+ Recent posts