Skip to main content

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 datacontext then this property is not required.

Bining 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 objects, 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 the 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. WPF Binding Mode
  2. WPF UpdateSourceTrigger
  3. WPF INotifyPropertyChanged Interface

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

Comments

  1. How to make money from casino games - Work Tomake Money
    Money management is based on a process of หาเงินออนไลน์ taking a gambler and making money in casino games. Once that choegocasino process has been 바카라 사이트 completed, we have to

    ReplyDelete

Post a Comment

Popular posts from this blog

Generate QR Code in WPF

Introduction In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application. Getting Started Here in the demonstration Generate QR Code in WPF , will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code. As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text. Generate QR Code in WPF Here are the steps to generate QR code and let's follow the steps to complete demonstrations. Demonstration:- Generate QR Code Open visual stud

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class