2013年2月3日日曜日

DataBinding(その2)

Mode(方向)とTrigger(きっかけ)について
   Binding によるデータの連携には、データの方向と更新タイミングの指定が必要

1.BindingのMode(方向)とは、データ更新の方向を指定すること

       [System.Windows.Data.BindingMode]  ←(Enum型)

 
名前 方向 内容
OneWay (S→T) 即更新
TwoWay (S→T) 即更新
   〃 (S←T) UpdataSourceTrigger値に依存
OneWayToSource (S←T) 同上(ソースが非依存関係プロパティのとき)
OneTime (S→T) 初期設定された時に1回のみ
Default (S→T) Element毎に定義されている(例:TextBoxはTwoWay)

2.BindingのTrigger(きっかけ)とは、データ更新のタイミングを指定すること

      [System.Windows.Data.UpdateSourceTrigger]    ←(Enum型)

名前 内容
Default ?
PropertyChanged ソース値が変化したとき
LostFocus ターゲット値が変化し、フォーカスを失ったとき
Explicit ソース値が変化し、明示的にBindingExpressionの
UpdateSource()が実行されたとき

3.改めて実際の例で、「TextBox」Bindingインスタンスに、Mode(方向)と
     Trigger(きっかけ)が設定されているか見てみることにする

実際の例

image

   1: #------------------------------------------------------ 
   2: if ($host.Version.Major -eq 3) {
   3:     powershell.exe
   4: } else {
   5:     powershell.exe -version 2 -sta
   6: }
   7:  
   8: if ($host.Version.Major -eq 3) {
   9:     add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml,System.xaml
  10: } else {
  11:     add-type -assembly WindowsBase,PresentationCore,PresentationFramework
  12: }
  13:  
  14: $Xaml = @'
  15: <Window
  16:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  17:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  18:     Title="Markup Extensions" Height="220" Width="175">
  19:  
  20:     <Window.Resources>
  21:         <LinearGradientBrush x:Key="gradBrush" StartPoint="0,0" EndPoint="1,1"> 
  22:             <GradientStop Color="SteelBlue" Offset="0" />
  23:             <GradientStop Color="Silver" Offset="1"/>
  24:         </LinearGradientBrush>
  25:     </Window.Resources>
  26:  
  27:     <StackPanel Background="{StaticResource gradBrush}">
  28:         <TextBox Name="tbValue" Margin="10"
  29:                  Text="{Binding ElementName=sldrSlider, Path=Value, Mode=TwoWay}"/>
  30:         <Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/>
  31:         <Button Name="bnValue">Trigger</Button>
  32:     </StackPanel>
  33:  
  34: </Window>
  35: '@
  36:  
  37: $form = [System.Windows.Markup.XamlReader]::Parse($xaml)
  38:  
  39: $script = {
  40:     $bindExpression = $form.FindName("tbValue").GetBindingExpression(`
  41:                       [System.Windows.Controls.TextBox]::TextProperty)
  42:     $bindExpression.UpdateSource()
  43: }
  44:  
  45: $form.findName("bnValue").add_Click($script)
  46: $Form.ShowDialog() | out-null

「TextBox」の「Text依存関係プロパティ」につなげられている「Slider」の値を見る


> $form.findname("tbValue").GetBindingExpression  ( `
            [System.Windows.Controls.TextBox]::TextProperty).ParentBinding


      > Mode                              TwoWay ←★1
      > UpdateSourceTrigger    Default ←★2

このように、「Binding」インスタンスにMode(方向)とTrigger(タイミング)が保持されている。

0 件のコメント:

コメントを投稿