Introduction:
In this article i am going to explain about how to create ui element property binding (Dependency property) in wpf.
Main:
The System.Windows.Data.Binding namespace creates a relationship between two properties: a binding source and a
binding target. In this case, the target is the property of the element with the value you want to set. The source
is the property of the element you want to get the value from. The target property must be a System.Windows.DependencyProperty, is designed to support data binding.
In the XAML for the property you want to set, declare a Binding statement inside curly braces. Set the ElementName
attribute to the name of the element to use as the binding source object. Set the Path attribute to the property on the source where the data should come from.
for ex,
<Window x:Class="Dependpropdemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Slider Name="slider"
Margin="4" Interval="1"
TickFrequency="1"
IsSnapToTickEnabled="True"
Minimum="0" Maximum="100"/>
<StackPanel Orientation="Horizontal" >
<TextBlock Width="Auto" HorizontalAlignment="Left" Margin="4"
Text="The value property of the slider is:" />
<TextBlock Width="40" HorizontalAlignment="Center" Margin="4"
Text="{Binding
ElementName=slider,
Path=Value}" />
</StackPanel>
</StackPanel>
</Window>
<Window x:Class="Dependpropdemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel> <Slider Name="slider" Margin="4" Interval="1" TickFrequency="1" IsSnapToTickEnabled="True" Minimum="0" Maximum="100"/> <StackPanel Orientation="Horizontal" > <TextBlock Width="Auto" HorizontalAlignment="Left" Margin="4" Text="The value property of the slider is:" /> <TextBlock Width="40" HorizontalAlignment="Center" Margin="4" Text="{Binding ElementName=slider, Path=Value}" /> </StackPanel> </StackPanel> </Window> |
Conclusion:
Hope this helps,
Happy coding.


Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!
KOVAL ! why do you only respond to people who threaten to unsubscribe… what about me….Id like a shout out too ….I watched all your videos….TWICE…..i loved you when you weren’t? famous…. *sigh*