INotifyPropertyChanged Arayüzü

Bu makalemizde INotifyPropertyChanged arayüzünün ne ise yaradigini, nasil tanimlandigini ve nasil implement edildiginden bahsedecegiz. INotifyPropertyChanged arayüzü System.Component altinda gelir. Özellikle Wpf’te ( Windows Presentation Foundation) sikça kullanilir. INotifyPropertyChanged arayüzü bir property’nin degerinin degistigi zaman arayüze bildirim yapilmasini saglar.

public class Client : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  public Client(string name, bool selected)
  {
   this.name = name;
   this.selected = selected;
  }
  private string name;
  public string Name
  {
   get
   {
    return this.name;
   }
   set
   {  
    this.name = value;    
   }
  }
  private bool selected;
  public bool Selected
  {
   get
   {
    return this.selected;
   }
   set
   { 
    this.selected = value;   
    this.PropertyChanged(this, new PropertyChangedEventArgs("Selected"));
   }
  }
}

 


.

Burada Client adinda bir sinif tanimladik ve bu sinifin INotifyPropertyChanged arayüzünü implement edecegini belirttik. Bu sinifta kullanici arayüzünde kullanmak üzere Name ve Selected adlarinda 2 tane property ve INotifyPropertyChanged implement etmek adina PropertyChanged adinda PropertyChangedEventHandler eventi tanimladik. Selected propertinin set isleminde ise, PropertyChangedEventHandler eventini ayaga kaldirdik.

<Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525">
  <Grid>
    <StackPanel>
      <CheckBox Name="chkAll" Content="Hepsini Isaretle"
  Checked="chkAll_Checked" Unchecked="chkAll_Unchecked" Margin="0,0,0,3"/>
    <ListBox ItemsSource="{Binding ListeKullaniciYetki}" Name="listBox2">
      <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
          <CheckBox Content="{Binding Name}"
          IsChecked="{Binding Selected}"/>
        </HierarchicalDataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    </StackPanel>
  </Grid>
</Window>

Xaml dosyamizda bir tane listbox tanimladik ve listbox’in elemanlarinin checkbox kontrollerinden olusacagini belirttik. Bu checkboxlarin Content propertylerinin Name adli bir propertyden, IsChecked propertilerinin ise Selected adli bir propertiden deger alacagini belirttik.


.
public partial class MainWindow : Window
 {
  private List listeKullaniciYetki;
  public List ListeKullaniciYetki
  {
   get
   {
    if (listeKullaniciYetki == null)
    {
     listeKullaniciYetki = new List();
    }
    return listeKullaniciYetki;
   }
  }
  public MainWindow()
  {
   InitializeComponent();
   ListeKullaniciYetki.Add(new Client("Sezgin Zeka", false));
   ListeKullaniciYetki.Add(new Client("Koray Korkmaz", false));
   ListeKullaniciYetki.Add(new Client("Kivanç Ali Tosunpasa", false));
   ListeKullaniciYetki.Add(new Client("Enis Kahyaogullari", false));
   ListeKullaniciYetki.Add(new Client("Emre Küçüktilki", false));
   listBox2.ItemsSource = ListeKullaniciYetki;
  }
  private void chkAll_Checked(object sender, RoutedEventArgs e)
  { 
   ListeKullaniciYetki.ForEach(x => x.Selected = true);
  }
  private void chkAll_Unchecked(object sender, RoutedEventArgs e)
  { 
   ListeKullaniciYetki.ForEach(x => x.Selected = false);
  }
 }

Client tipinde bir tane liste olusturup, bu listenin içerisine 4 tane Client nesnesi olusturup ekledikten sonra ListBox kontrolünün datasource propertisine atadik. Listbox’in üzerinde bulunan "hepsini isaretle" chekcbox’inin Checked ve UnChecked eventlerinde bu listenin içinde bulunan Client nesnelerinin Selected propertylerine true ve false degerlerini atadik. Bu degerler atanir atanmaz INotifyPropertyChanged arayüzünü implement etmis olan Selected propertisi listbox içersinde bulunan checkboxlarin isaretlenip isaretlerinin kaldirilmasini interaktif sekilde sagliyor.

Eger Selected propertysinin içinde PropertyChangedEventHandler eventini ayaga kaldirmak yoluyla INotifyPropertyChanged arayüzünü implement etmemis olsaydik, "hepsini isaretle" checkboxini isaretleseydik veya "hepsini isaretle" checkboxindan sareti kaldirsaydik listbox’ta bulunan checkboxlarda herhangi bir degisiklik olmayacakti.


.

Proje Kaynak kodlari
Sezgin Zeka
sezginzeka@gmail.com

2 thoughts on “INotifyPropertyChanged Arayüzü

  1. Ellerinize sa?lyk çok güzel olmu? ama Code Behind kysmynda ListeKullaniciYetki isimli property nin tipini List yapmy?synyz. Onun List<Client> tipinde olmasy gerekiyor. Sorun çykarabilir 🙂

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir