Yazinin ilk bolumunde Window classinda tanimlanmis bir propertye UI’dan nasil bind edildigini anlattim. Onceki ornekteki class’i kullanarak simdi de code dan bu property’i degistirdigimizde neler oluyor ona bakalim.
|
public class MyData { public string Name { get; set; } public string Surname { get; set; } } public partial class Main : Window { public MyData MyDataProp { get; set; } public Main() { MyDataProp = new MyData { Name = “Bill”, Surname = “Gates” }; InitializeComponent(); } } |
Xaml kodu da asagidaki gibidir.
|
<Window x:Class=”dinceru.wordpress.com.PropertyWPF.Main” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”Main” Height=”Auto” Width=”300″ Name=”Mainw”> <Grid HorizontalAlignment=”Stretch” VerticalAlignment=”Top” > <Grid.RowDefinitions> <RowDefinition Height=”68*”></RowDefinition> <RowDefinition Height=”68*”></RowDefinition> <RowDefinition Height=”68*”></RowDefinition> </Grid.RowDefinitions> <Grid.DataContext> <Binding ElementName=”Mainw” Path=”MyDataProp”/> </Grid.DataContext> <StackPanel Grid.Row=”0″> <TextBlock>Name :</TextBlock> <TextBox Name=”textbox1″ Text=”{Binding Path=Name, UpdateSourceTrigger=LostFocus}” Width=”100″ Height=”20″ HorizontalAlignment=”Left” VerticalAlignment=”Top” /> </StackPanel> <StackPanel Grid.Row=”1″> <TextBlock>Surname :</TextBlock> <TextBox Name=”textBox2″ Text=”{Binding Path=Surname, UpdateSourceTrigger=PropertyChanged}” Width=”100″ Height=”20″ HorizontalAlignment=”Left” VerticalAlignment=”Top” /> </StackPanel> <StackPanel Grid.Row=”2″> <Button Height=”23″ Name=”button1″ Width=”225″ Click=”button1_click” Content=”Change property value from code behind” /> </StackPanel> </Grid> </Window> |
button1_click event handlerinda MyDataProp u degistirelim
|
public void button1_click(object sender, RoutedEventArgs e) { MyDataProp.Name = “King of”; MyDataProp.Surname = “Geeks”; } |
![]() |
Yandaki uygulamada butona clickledikten sonra propertyi degisse bile bu degisiklik controllere yansimamakta cunku bu sefer source update oldu, target in updatei gerekli bunu yapmak icin 2 yontem var aklima gelen, birincisi fazla kullanilmiyor o da : Ilk yazida source un update i icin kullandigimiz BindingExpression in bu seferde target update metodunu cagirarak oluyor. Asagidaki ornekteki kod bu islem icin yeterli |
|
public void button1_click(object sender, RoutedEventArgs e) { MyDataProp.Name = “King of”; MyDataProp.Surname = “Geeks”; BindingExpression be= this.textbox1.GetBindingExpression(TextBox.TextProperty); be.UpdateTarget(); BindingExpression be2 = this.textbox2.GetBindingExpression(TextBox.TextProperty); be2.UpdateTarget(); } |
Update islemini binding expression kullanmadan gerceklesmesi yani bind edilen propertye kod tarafinda yapilan her degisikligin UI tarafindan farkedilip guncellenmesi icin bind edilen Property inin INotifyPropertyChanged interfaceini implemente etmesi ve property degisikliklerinde bu interfacein tek elemani olan PropertyChangedEvent in raise edilmesi gerekmektedir.
Buna uygun yeni bir type yaratalim.
|
public class MyData2 : INotifyPropertyChanged { private string _name; private string _surname; public event PropertyChangedEventHandler PropertyChanged; private void RaiseChangedEvent(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } public string Name { get { return _name; } set { if (value != _name) { _name = value; RaiseChangedEvent(“Name”); } } } public string Surname { get { return _surname; } set { if (value != _surname) { _surname = value; RaiseChangedEvent(“Surname”); } } } } |
Window classimda MyDataProp2 tipinde bir property yaratip initialize edip, bunuda xaml da bind ediyorum.
|
public MyData MyDataProp { get; set; } public MyData2 MyDataProp2 { get; set; } public Main() { MyDataProp = new MyData { Name = “Bill”, Surname = “Gates” }; MyDataProp2 = new MyData2 { Name = “Bill”, Surname = “Gates” }; InitializeComponent(); } |
XAML da iki propertye de bind ediyorum ve ikinci propertyi de koddan degistirmek icin bir buton daha ekliyorum
|
<StackPanel Grid.Row=”0″> <TextBlock>Binding to MyDataProp</TextBlock> <TextBlock>Name :</TextBlock> <TextBox Name=”textbox1″ Text=”{Binding Path=Name}” Width=”100″ Height=”20″ HorizontalAlignment=”Left” VerticalAlignment=”Top” /> </StackPanel> <StackPanel Grid.Row=”1″> <TextBlock>Surname :</TextBlock> <TextBox Name=”textBox2″ Text=”{Binding Path=Surname}” Width=”100″ Height=”20″ HorizontalAlignment=”Left” VerticalAlignment=”Top” /> </StackPanel> <StackPanel Grid.Row=”2″> <Button Height=”23″ Name=”button1″ Width=”225″ Click=”button1_click” Content=”Change property value from code behind” /> </StackPanel> <StackPanel Grid.Row=”3″> <TextBlock>Binding to MyDataProp2 (implements INotifyPropertyChanged</TextBlock> <TextBlock>Name :</TextBlock> <TextBox Name=”textbox3″ Text=”{Binding ElementName=Mainw, Path=MyDataProp2.Name}” Width=”100″ Height=”20″ HorizontalAlignment=”Left” VerticalAlignment=”Top” /> </StackPanel> <StackPanel Grid.Row=”4″> <TextBlock>Surname :</TextBlock> <TextBox Name=”textBox4″ Text=”{Binding ElementName=Mainw, Path=MyDataProp2.Surname}” Width=”100″ Height=”20″ HorizontalAlignment=”Left” VerticalAlignment=”Top” /> </StackPanel> <StackPanel Grid.Row=”5″> <Button Height=”23″ Name=”button2″ Width=”225″ Click=”button2_click” Content=”Change property value from code behind” /> </StackPanel> |
Buttonlarin click eventleri:
|
public void button1_click(object sender, RoutedEventArgs e) { MyDataProp.Name = “King of”; MyDataProp.Surname = “Geeks”; BindingExpression be = this.textbox1.GetBindingExpression(TextBox.TextProperty); be.UpdateTarget(); BindingExpression be2 = this.textBox2.GetBindingExpression(TextBox.TextProperty); be2.UpdateTarget(); } public void button2_click(object sender, RoutedEventArgs e) { MyDataProp2.Name = “King of”; MyDataProp2.Surname = “Geeks”; } |
![]() |
Iki yontemda ayni sonucu vermekte tabi en iyi uygulama UI da bind edecegimiz objelerin herzaman INotifyPropertyChanged interfaceini implemente etmesi boylece property ister codedan ister UI dan degissin herzaman guncel oldugundan emin oluyoruz.Diger yontem ise belli durumlarda bir kerelik kullanimlarin gerektigi durumlarda uygun olabilir. |
Ucuncu bolumde gorusmek uzere.
Anlatilanlar icin kaynak koda buradan ulasabilirsiniz.
Dincer Uyav

