ページ

2011年10月30日日曜日

◆WPF FirstBinding

[WPF]レイアウトに飽きてきたのでバインディングしてみる

サンプルは基本的に上記サイトからそのまま引用させてもらっている。

<XAML>

  1. <Window x:Class="DockPanel.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.     <Grid>  
  6.         <Grid.RowDefinitions>  
  7.             <RowDefinition Height="Auto" />  
  8.             <RowDefinition Height="Auto" />  
  9.             <RowDefinition Height="Auto" />  
  10.         </Grid.RowDefinitions>  
  11.         <Grid.ColumnDefinitions>  
  12.             <ColumnDefinition Width="Auto" />  
  13.             <ColumnDefinition Width="Auto" />  
  14.         </Grid.ColumnDefinitions>  
  15.           
  16.         <Label Content="値:" Grid.Row="0" Grid.Column="0" />  
  17.         <Label Name="valueLabel" Content="ここに値が来ます" Grid.Row="0" Grid.Column="1" />  
  18.         <Label Content="概要:" Grid.Row="1" Grid.Column="0" />  
  19.         <Label Name="descriptionLabel" Content="ここに概要が来ます" Grid.Row="1" Grid.Column="1" />  
  20.         <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" >  
  21.             <Button Name="incrButton" Content="インクリメント" Margin="2" Click="incrButton_Click" />  
  22.             <Button Name="decrButton" Content="デクリメント" Margin="2" Click="decrButton_Click" />  
  23.             <Button Name="dumpButton" Content="Dump" Margin="2" Click="dumpButton_Click" />  
  24.         </StackPanel>  
  25.               
  26.     </Grid>  
  27. </Window>  

<ソース>


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14.   
  15. namespace DockPanel  
  16. {  
  17.     /// <summary>  
  18.     /// MainWindow.xaml の相互作用ロジック  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         Counter _counter;  
  23.         BindingExpressionBase _valueBindingExpression;  
  24.   
  25.         public MainWindow()  
  26.         {  
  27.             InitializeComponent();  
  28.   
  29.             //カウンタを作る  
  30.             _counter = new Counter(){Description="サンプルカウンター"};  
  31.             InitializeBinding();  
  32.         }  
  33.   
  34.         private void InitializeBinding()  
  35.         {  
  36.             Binding valueBinding = new Binding("Value");  
  37.             _valueBindingExpression = BindingOperations.SetBinding(  
  38.                 valueLabel, Label.ContentProperty, valueBinding);  
  39.   
  40.             Binding descriptionBinding = new Binding("Description");  
  41.             BindingOperations.SetBinding(  
  42.                 descriptionLabel, Label.ContentProperty, descriptionBinding);  
  43.   
  44.             // カウンタをDataContextに!  
  45.             this.DataContext = _counter;  
  46.         }   
  47.   
  48.         class Counter  
  49.         {  
  50.             public int Value { get; private set; }  
  51.             public string Description { get; set; }  
  52.   
  53.             public void Incr()  
  54.             {  
  55.                 Value++;  
  56.             }  
  57.             public void Decr()  
  58.             {  
  59.                 Value--;  
  60.             }  
  61.         }  
  62.   
  63.         private void incrButton_Click(object sender, RoutedEventArgs e)  
  64.         {  
  65.             _counter.Incr();  
  66.             _valueBindingExpression.UpdateTarget();  
  67.   
  68.         }  
  69.   
  70.         private void decrButton_Click(object sender, RoutedEventArgs e)  
  71.         {  
  72.             _counter.Decr();  
  73.             _valueBindingExpression.UpdateTarget();  
  74.   
  75.         }  
  76.   
  77.         private void dumpButton_Click(object sender, RoutedEventArgs e)  
  78.         {  
  79.             // カウンタの中身を確認  
  80.             MessageBox.Show(string.Format("{0}: {1}", _counter.Value, _counter.Description));  
  81.         }  
  82.     }  
  83. }  

バインディングの構文
image


データソースとしてここでは、DataContextに指定したCounterオブジェクトを使用している。
そのDataContextのプロパティ名でBindingオブジェクトを作ってSetBindingするというパターンのようだ。


ソース値の変更を反映するには、値を変更したタイミングでUpdateTargetメソッドを呼んであげる。


Updateを自動的に行うにはデータソースのクラスに
INotifyPropertyChangedインタフェースを実装する。


このインタフェースは以下のイベントの実装を要求する。
event PropertyChangedEventHandler PropertyChanged


イベントを追加したCounterクラスを以下に引用する。

  1. class Counter:INotifyPropertyChanged  
  2. {  
  3.     private int value;  
  4.     public int Value  
  5.     {  
  6.         get { return value; }  
  7.         set  
  8.         {  
  9.             this.value = value;  
  10.             OnPropertyChanged("Value");  
  11.         }  
  12.     }  
  13.   
  14.     private string description;  
  15.     public string Description  
  16.     {  
  17.         get { return description; }  
  18.         set  
  19.         {  
  20.             this.description = value;  
  21.             OnPropertyChanged("Description");  
  22.         }  
  23.     }  
  24.   
  25.     public void Incr()  
  26.     {  
  27.         Value++;  
  28.     }  
  29.     public void Decr()  
  30.     {  
  31.         Value--;  
  32.     }  
  33.   
  34.     public event PropertyChangedEventHandler PropertyChanged;  
  35.     protected void OnPropertyChanged(string name)  
  36.     {  
  37.         if (PropertyChanged != null)  
  38.         {  
  39.             PropertyChanged(thisnew PropertyChangedEventArgs(name));  
  40.         }  
  41.     }  
  42. }  

プロパティが沢山あったら書くのが面倒くさそう。
ほとんどが定型的な処理なので何かしらVSのサポートがあったもよさそうなものだが・・・。
(WPFが浸透して行かない理由の1ではなかろうか。)


個人的には文字列指定の多用も好きになれない。(リフレクションでゴネゴネするらしいが)

0 件のコメント:

コメントを投稿

私が最近チェックした記事