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 markup after the Button element inside the horizontally orientated StackPanel:

    <Slider Value="50" Maximum="100" Minimum="0"  
      Width="200" Name="slider"/> 
    <TextBlock Text="{Binding ElementName=slider, Path=Value}"  
      VerticalAlignment="Center" Margin="10"/> 

Rerun the app. Click, hold, and drag the slider, and notice that the text block shown to the right of the slider always shows the current value of the slider:

Binding to elements

Under the horizontally orientated stack ...

Get C# 7 and .NET Core now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.