WPFにあってSilverlight 2 Betaにないシリーズ「Binding.ElementNameプロパティ」
Binding.ElementName プロパティ (System.Windows.Data)
このプロパティは、アプリケーション内の別の要素のプロパティにバインドするときに役立ちます。たとえば、Sliderを使用してアプリケーション内の別のコントロールの高さを制御する場合や、コントロールのContentをListBoxコントロールのSelectedValueプロパティにバインドする場合に指定します。
たとえば、ElementNameプロパティを使うことで、SliderのValueプロパティとTextBoxのTextプロパティをバインドさせることができました。
XAML( WPF )
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="5">
<TextBox Name="textBox"/>
<Slider Value="{Binding ElementName=textBox, Path=Text}" Maximum="100" />
</StackPanel>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="5">
<TextBox Name="textBox"/>
<Slider Value="{Binding ElementName=textBox, Path=Text}" Maximum="100" />
</StackPanel>
</Window>
Silverlight 2 BetaにはBindingクラスにElementNameプロパティがありませんので、上記のような書き方はできません。しかしながら、下記のblogにてヘルパークラスを作成する方法で要素間バインディングを実現する方法が掲載されていましたので、それを紹介したいと思います。
Workaround for missing ElementName in Silverlight 2.0 Binding
同じコードを書いても仕方がないので、ここではいつものようにVisual Basicのコードに書き直したものを載せておきます。
Visual Basic( Silverlight 2 Beta )
Imports System.ComponentModel
Public Class BindingHelper
Implements INotifyPropertyChanged
Private _value As Object
Public Property Value() As Object
Get
Value = _value
End Get
Set(ByVal value As Object)
_value = value
OnPropertyChanged("Value")
End Set
End Property
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Public Class BindingHelper
Implements INotifyPropertyChanged
Private _value As Object
Public Property Value() As Object
Get
Value = _value
End Get
Set(ByVal value As Object)
_value = value
OnPropertyChanged("Value")
End Set
End Property
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
XAML( Silverlight 2 Beta )
<UserControl x:Class="BindingHelperSample.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:BindingHelperSample"
Width="400" Height="300">
<StackPanel Margin="5" Background="White">
<StackPanel.Resources>
<src:BindingHelper x:Key="bindingHelper" />
</StackPanel.Resources>
<TextBox Text="{Binding Value, Source={StaticResource bindingHelper}}" />
<Slider Value="{Binding Value, Source={StaticResource bindingHelper}, Mode=TwoWay}" Maximum="100" />
</StackPanel>
</UserControl>
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:BindingHelperSample"
Width="400" Height="300">
<StackPanel Margin="5" Background="White">
<StackPanel.Resources>
<src:BindingHelper x:Key="bindingHelper" />
</StackPanel.Resources>
<TextBox Text="{Binding Value, Source={StaticResource bindingHelper}}" />
<Slider Value="{Binding Value, Source={StaticResource bindingHelper}, Mode=TwoWay}" Maximum="100" />
</StackPanel>
</UserControl>