WPFバインディング
WPF バインディング
まずは用語から、
バインディングソースになれるもの
XAMLのみでのBinding
box2のTextをbox1のTextにバインド。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Binding1" Height="100" Width="100"> <StackPanel> <TextBox Name="box1" Text="{Binding ElementName=box2, Path=Text, Mode=TwoWay}"/> <TextBox Name="box2"/> </StackPanel> </Window>
スライダーの例
XAML
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Markup Extensions" Height="220" Width="175"> <StackPanel> <TextBox Name="tbValue" Margin="10" Text="{Binding ElementName=sldrSlider, Path=Value, Mode=TwoWay}"/> <Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/> <Button Name="bnValue">Trigger</Button> </StackPanel> </Window>
PS
if ($host.Version.Major -eq 3) { powershell.exe } else { if ($host.Runspace.ApartmentState -eq "STA") {return} powershell.exe -version 2 -sta } 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 } $Xaml_win = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Markup Extensions" Height="220" Width="175"> <StackPanel> <TextBox Name="tbValue" Margin="10" Text="{Binding ElementName=sldrSlider, Path=Value, Mode=TwoWay}"/> <Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/> <Button Name="bnValue">Trigger</Button> </StackPanel> </Window> '@ $form = [System.Windows.Markup.XamlReader]::Parse($xaml_win) $script = { $bindExpression = $form.FindName("tbValue").GetBindingExpression(` [System.Windows.Controls.TextBox]::TextProperty) $bindExpression.UpdateSource() } $form.findName("bnValue").add_Click($script) [void]$Form.ShowDialog() exit
C#を使った例
if ($host.Version.Major -eq 3) { powershell.exe } else { if ($host.Runspace.ApartmentState -eq "STA") {return} powershell.exe -version 2 -sta } $xaml_win = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Markup Extensions" Height="220" Width="175"> <StackPanel> <Label Name="displayText" Margin="5"/> <TextBox Name="sourceInfo">10</TextBox> </StackPanel> </Window> '@ # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり $code = @' using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Data; namespace MyBind { public class BindingClass { public void bindData(object source, object dest) { TextBox s1 = (TextBox)source; Label d1 = (Label)dest; //Bindingの生成 Binding bindingShared = new Binding(); // SourceとPathの設定 bindingShared.Source = s1; bindingShared.Path = new PropertyPath( "Text" ); //Bindingオブジェクトのセット d1.SetBinding( Label.ContentProperty, bindingShared ); d1.SetBinding( Label.FontSizeProperty, bindingShared ); } } } '@ # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり if ($host.Version.Major -eq 3) { Add-Type $code -ReferencedAssemblies @("WindowsBase","PresentationCore","PresentationFramework","system.xaml") } else { Add-Type $code -ReferencedAssemblies @("WindowsBase","PresentationCore","PresentationFramework") } $form = [System.Windows.Markup.XamlReader]::Parse($xaml_win) $textBox = $form.FindName("sourceInfo") $label = $form.FindName("displayText") $mb = new-object MyBind.BindingClass $mb.bindData($textBox, $label); [void]$Form.ShowDialog() exit
参考
C# binding
http://www.atmarkit.co.jp/ait/spv/1010/08/news123.html
PowerShell Binding Wxample
http://harebare01.blogspot.jp/2013/01/binding.html?m=1
Markup extension
http://harebare01.blogspot.jp/2013/01/blog-post_3.html?m=1
https://msdn.microsoft.com/ja-jp/library/ms743643(v=vs.110).aspx