WPF Binding

Introduction

This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind.

Getting Started

Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source, and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well.

XAML Example

 <TextBox x:Name="sourceText" Grid.Row="0" />   
 <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/>  

Code Example

 Button btn=new Button();  
 Binding binding = new Binding("Text");   
 binding.Source = sourceText;   
 btn.SetBinding(Button.ContentProperty, binding);  

In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to discuss that elements. We mostly use these properties of binding i.e ElementName,Path,Mode,UpdateSourceTrigger,Converter,RelativeSource.

Binding ElementName

ElementName is required only when you bind two WPF UI Controls it gets or sets the name of the element to use as the binding source object, if you are binding property of data context then this property is not required.

Binding Path

The part property of binding is for getting and setting the property of the source. let's say if you want to bind the text property of text box with text property of text block, then when you using binding in text block you have to mention the text property of text box in the path of binding. See the example below.

 <TextBox Name="txtinput" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="416"/>  
     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ElementName=txtinput, Path=Text}" VerticalAlignment="Top" Margin="0,39,0,0"/>  

Binding Mode

The binding mode defines how data will flow from one object to another object. There are 5 different modes available in WPF listed below.

  1. OneWay:- Causes changes to the source property to automatically update the target property but the source does not get changed.
  2. TwoWay:- Changes in the source or target automatically cause updates to the other.
  3. OneTime: Causes only the first time change to the source property to automatically update the target property but the source does not get changed and subsequent changes do not affect the target property.
  4. OneWayToSource:-Causes changes to the target property to automatically update the source property but the target does not get changed.
  5. Default:-

    The default is Default, which returns the default binding mode value of the target dependency property. However, the default value varies for each dependency property. In general, user-editable control properties, such as those of text boxes and checkboxes, default to two-way bindings, whereas most other properties default to one-way bindings.

To support OneWay and TwoWay bindings, the underlying data must implement INotifyPropertyChanged and for TwoWay or OneWayToSource bindings, you can control the target-to-source updates by setting the UpdateSourceTrigger property.

UpdateSourceTrigger

UpdateSourceTrigger decides when the data should get updated between WPF objects that are binded. There are 4 different modes by which UpdateSourceTrigger can be defined:-

  1. Default: - If it’s a text property then data is updated during lost focus and for normal properties data updates in property change event.
  2. PropertyChanged: - In this setting data is updated as soon as the value is changed.
  3. LostFocus: - In this setting data is updated as soon as lost focus event occurs.
  4. Explicit: - In this setting the data is updated manually. In other words, to update data between two WPF object you need to call the below code.

Converter

In WPF binding data flows from source to target and vice versa, the WPF Converters acts as a bridge between the source and the target if the source and target have different data formats or need some conversion. For example, sometimes we need to convert data from one format to another format, when it flows from the source to the target or vice-versa the conversion is required. Thre are two types of converter available in WPF.

  • ValueConverters
  • MultiValueCoverters

For more details about converter go to the link

RelativeSource

This relative resource helps to bind to one property of an element to the other property of the same element. For example, in the below XAML the border width is bound to height of the same border element.

     <Border BorderBrush="Gray" BorderThickness="1" Height="139" Width="{Binding Height, RelativeSource={RelativeSource Self}}">  
       <TextBox Name="txtinput" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="416" Background="{Binding Path=BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Border} }}"/>  
     </Border>  

Related Articles

  1. Resource in WPF
  2. WPF Style
  3. WPF Triggers
  4. WPF Converters
  5. UpdateSourceTrigger
  6. WPF Binding Mode

Summary

in the above we have discussed details about WPF binding and their most-used properties, Hope you have understood what we have detail ideas about WPF binding and their properties.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال