Saturday, June 16, 2012

WPF Binding a Control to Another Control

Today I'll show how to bind a WPF control to another one directly in XAML.
In this example I have a Rectangle control and a Slider.
I want to change Rectangle's width by moving the Slider.
WPF Binding Between Controls Directly in XAML
Let's take a look on XAML:
<Grid>
    <StackPanel>            
        <Rectangle Name="elipse1" 
                   Fill="Black"
                   Margin="10,40"
                   Height="40"
                   Width="{Binding ElementName=slider1, Path=Value}"/>
        <Slider Name="slider1" 
                Minimum="20" 
                Maximum="200"
                Margin="20,0"/>            
    </StackPanel>
</Grid>
The main thing here is actually this string:
    Width="{Binding ElementName=slider1, Path=Value}"
Pretty simple example but I believe it will help to someone.
You are welcome to download the source code (Visual Studio 2010).

1 comment:

  1. This is exactly the code that I looked for!

    ReplyDelete