Data binding
When building graphical user interfaces, you will often want to bind a property of one control to another or to some data.
Binding to elements
In the MainWindow.xaml
file, add the following elements after the Button
element, inside the horizontal StackPanel
:
<Slider Value="50" Maximum="100" Minimum="0" Width="200" Name="slider"/> <TextBlock Text="{Binding ElementName=slider, Path=Value}" VerticalAlignment="Center" Margin="10"/>
Redeploy and then run the app. Click and drag the slider, and notice that the text block always shows the current value of the slider:
Under the horizontal stack panel, add these statements:
<Rectangle Height="100" Width="100" Fill="Red"> <Rectangle.RenderTransform> <RotateTransform Angle="{Binding ElementName=sliderRotation, Path=Value}" /> </Rectangle.RenderTransform> </Rectangle> <TextBlock>Use the slider to rotate the square:</TextBlock> <Slider Value="0" Minimum="0" Maximum="360"...