Üye Kayıt Üye Giriş

INotifyPropertyChanged Arayüzü


INotifyPropertyChanged Arayüzü

 

Bu makalemizde INotifyPropertyChanged arayüzünün ne işe yaradığını, nasıl tanımlandığını ve nasıl implement edildiğinden bahsedeceğiz. INotifyPropertyChanged arayüzü System.Component altında gelir. Özellikle Wpf'te ( Windows Presentation Foundation) sıkça kullanılır. INotifyPropertyChanged arayüzü bir property'nin değerinin değiştigi zaman arayüze bildirim yapilmasını sağlar.

 

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 adında bir sınıf tanımladık ve bu sınıfın INotifyPropertyChanged arayüzünü implement edeceğini belirttik. Bu sınıfta kullanıcı arayüzünde kullanmak üzere Name ve Selected adlarında 2 tane property ve INotifyPropertyChanged implement etmek adına PropertyChanged adında PropertyChangedEventHandler eventi tanımladık. Selected propertinin set işleminde ise, PropertyChangedEventHandler eventini ayağa kaldırdık.

 

<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 İşaretle"
  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 dosyamızda bir tane listbox tanımladık ve listbox'ın elemanlarının checkbox kontrollerinden oluşacağını belirttik. Bu checkboxların Content propertylerinin Name adlı bir propertyden, IsChecked propertilerinin ise Selected adlı bir propertiden değer alacağını 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("Kıvanç Ali Tosunpaşa", false));
   ListeKullaniciYetki.Add(new Client("Enis Kahyaoğulları", 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 oluşturup, bu listenin içerisine 4 tane Client nesnesi oluşturup ekledikten sonra ListBox kontrolünün datasource propertisine atadık. Listbox'ın üzerinde bulunan "hepsini işaretle" chekcbox'ının Checked ve UnChecked eventlerinde bu listenin içinde bulunan Client nesnelerinin Selected propertylerine true ve false değerlerini atadık. Bu değerler atanır atanmaz INotifyPropertyChanged arayüzünü implement etmiş olan Selected propertisi listbox içersinde bulunan checkboxların işaretlenip işaretlerinin kaldırılmasını interaktif şekilde sağlıyor.

Eğer Selected propertysinin içinde PropertyChangedEventHandler eventini ayağa kaldırmak yoluyla INotifyPropertyChanged arayüzünü implement etmemiş olsaydık, "hepsini işaretle" checkboxını işaretleseydik veya "hepsini işaretle" checkboxından şareti kaldırsaydık listbox'ta bulunan checkboxlarda herhangi bir değişiklik olmayacaktı.


 

Bilgisayar Dershanesi Ders Sahibi;
Bilgisayar Dershanesi

Yorumlar

Yorum Yapabilmek İçin Üye Girişi Yapmanız Gerekmektedir.

ETİKETLER