다음을 통해 공유


INotifyPropertyChanged

Silverlightでは自分のプロパティが変化したとき、他のオブジェクトに伝えるINotifyPropertyChangedがUIElementに実装されていません。ですから、XAMLだけでスライダを使って矩形のサイズを変更させるようなことができません。

JoyOfCodeにINotifyPropertyChangedを使ったBindingHelperの実装例がありました。これを使うと、先のスライダと矩形がXAMLでかけます。ここではSilverlightにはElementNameがないので、その対応策だとも書いてあります。

スライダ→BindingHelper→Rectangleという2つのバインディングを使っています。スライダからHelperには逆のバインディングなので、TwoWayモードを使います。

<StackPanel Margin="5" Background="White">

  <StackPanel.Resources>

    <local:BindingHelper x:Key="bindingHelper" />

  </StackPanel.Resources>

  <Rectangle Height="50" Fill="Blue"

   Width="{Binding Value,

            Source={StaticResource bindingHelper}}"/>

  <Slider Maximum="100" Width="200"

   Value="{Binding Value,

            Source={StaticResource bindingHelper},

            Mode=TwoWay}"/>

</StackPanel>