データ・バインディングとは?
「Binding」クラス・インスタンス値を、マークアップ拡張機能を使って
指定した依存関係プロパティへデータ値としてセットする機能
| (Xamlな)マークアップ拡張機能 |
| ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ |
| <TextBox Margin="10" Text="{Binding ElementName=sldrSlider, Path=Value}"/> |
| ④ ① ② ③ |
①Bindingクラス名(System.Windows.Data.Binding)
②初期化パラメータ1
:ElementNameプロパティに(名前付けされた)ソースエレメント名を指定
③初期化パラメータ2
:Pathプロパティに(名前付けされた)ソースエレメントのプロパティを指定
④TextBoxエレメントのText依存関係プロパティに指定する
例 ← 「Slider」の値をTextBoxに数値として表示
(コード)
#------------------------------------------------------
if ($host.Version.Major -eq 3) {
powershell.exe
} else {
powershell.exe -version 2 -sta
}
$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Markup Extensions" Height="120" Width="175">
<StackPanel>
<TextBox Margin="10" Text="{Binding ElementName=sldrSlider, Path=Value}"/>
<Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/>
</StackPanel>
</Window>
'@
if ($host.Version.Major -eq 3) {
add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml,System.Xaml
} else {
add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml
}
$form = [System.Windows.Markup.XamlReader]::Parse($xaml)
$Form.ShowDialog() | out-null
#設定値を表示してみる。
$form.content.children[0].text #<--TextBlockエレメントの文字列
受け取り側TextBoxのすべての依存関係プロパティ(除:Inheritなもの)
(> $form.Content.Children[0] | dp2: )
| Name | DP Type | Value | base | |
| =============== | ========== | ========================== | ==== | |
| IsMouseDirectlyOver | Indirect,Xaml | False | Local | |
| IsKeyboardFocused | Indirect,Xaml | False | Local | |
| ★ | Text | WPF Binding | 4.67741935483871 | Local |
| IsMouseCaptured | Indirect,Xaml | False | Local | |
| PageHeight | Indirect,Xaml | 18 | Local | |
| Instance | Indirect,Xaml | System.Windows.Documents.TextEditor | Local | |
| UndoManagerInstance | Indirect,Xaml | MS.Internal.Documents.UndoManager | Local | |
| Margin | WPF Direct | 10,10,10,10 | Local | |
| IsSelectionActive | Indirect,Xaml | False | Local |
送り出し側のSliderのすべての依存関係プロパティ(除:Inheritなもの)
(> $form.findname("sldrSlider") | dp2: )
| Name | DP Type | Value | base | |
| =============== | ========== | ========================== | ==== | |
| Margin | WPF Direct | 10,10,10,10 | Local | |
| IsKeyboardFocused | Indirect,Xaml | False | Local | |
| Name | WPF Direct | sldrSlider | Local | |
| ☆ | Value | WPF Direct | 4.67741935483871 | Local |
| PageHeight | Indirect,Xaml | 18 | Local | |
| Instance | Indirect,Xaml | System.Windows.Documents.TextEditor | Local | |
| UndoManagerInstance | Indirect,Xaml | MS.Internal.Documents.UndoManager | Local | |
| TickPlacement | WPF Direct | TopLeft | Local |
Bidingインスタンスを見てみる
改めて、上記例で以下の流れでBindingをたどってみる <--逆からは辿れない
> ターゲット・コントロール -> 依存関係プロパティ -> BindingExpression –> Binding
① ② ③ ④
①ターゲットコントロール
> $form.Content.Children[0]
②依存関係プロパティ
> $dp = [System.Windows.Markup.Primitives.MarkupWriter]::GetMarkupObjectFor(`
$form.Content.Children[0]).properties | Select-Object -First 1
| Property | Value | |
| ================= | ================================== | |
| Name | Text | |
| PropertyType | System.String | |
| PropertyDescriptor | MS.Internal.ComponentModel.DependencyObjectPropertyDescriptor | |
| IsAttached | False | |
| ★ | DependencyProperty | Text |
| Value | 4.67741935483871 | |
| Attributes | {System.CLSCompliantAttribute,Chars,MS.Internal.ComponentModel.DependencyPropertyAttribute,System.SerializableAttribute...} | |
| IsComposite | False | |
| StringValue | 4.67741935483871 | |
| Items | {System.Windows.Markup.Primitives.ElementMarkupObject} | |
| TypeReferences | {} | |
| IsConstructorArgument | False | |
| IsValueAsString | False | |
| IsContent | False | |
| IsKey | False |
③BindingExpressionインスタンス
> ($form.Content.Children[0]).GetBindingExpression($dp)
| Property名 | Value | |
| ============== | ================================================= | |
| ParentBinding | System.Windows.Data.Binding | |
| DataItem | System.Windows.Controls.Slider Minimum:0 Maximum:10 Value:4.67741935483871 | |
| ResolvedSource | System.Windows.Controls.Slider Minimum:0 Maximum:10 Value:4.67741935483871 | |
| ResolvedSourcePropertyName | Value | |
| ★ | Target | System.Windows.Controls.TextBox: 4.677 |
| ★ | TargetProperty | Text |
| ParentBindingBase | System.Windows.Data.Binding | |
| BindingGroup | ||
| Status | Active | |
| ValidationError | ||
| HasError | False | |
| HasValidationError | False | |
| IsDirty | False | |
| ValidationErrors |
④ちなみに、Bindingインスタンスも見てみる
> ($form.Content.Children[0]).GetBindingExpression($dp).ParentBinding
| Property名 | Value | |
| ================ | ================= | |
| ValidationRules | {} | |
| ValidatesOnExceptions | False | |
| ValidatesOnDataErrors | False | |
| ValidatesOnNotifyDataErrors | True | |
| Path | System.Windows.PropertyPath | |
| XPath | ||
| Mode | Default | |
| UpdateSourceTrigger | Default | |
| NotifyOnSourceUpdated | False | |
| NotifyOnTargetUpdated | False | |
| NotifyOnValidationError | False | |
| Converter | ||
| ConverterParameter | ||
| ConverterCulture | ||
| Source | ||
| RelativeSource | ||
| ☆ | ElementName | sldrSlider |
| IsAsync | False | |
| AsyncState | ||
| BindsDirectlyToSource | False | |
| UpdateSourceExceptionFilter | ||
| FallbackValue | {DependencyProperty.UnsetValue} | |
| StringFormat | ||
| TargetNullValue | {DependencyProperty.UnsetValue} | |
| BindingGroupName | ||
| Delay | 0 |
次は、DataBindingの「方向」と「トリガー」について