Social Icons

Wednesday, 21 August 2013

Data Binding with WPF : Binding to XML


In this article i will explain How to bind XML Data to Application UI in WPF. What are the different ways to specify the XML Data to Applicaiton, How to bind the xml elements to UI in wpf etc.

Download SourceCode and Video Explaining How to bind Xml Data to ListBox in WPF 


       In wpf for accessing xml data in your application you can use XmlDataProvider Class. This class provides declarative access to xml data for binding. XmlDataProvider Class provides three ways to access XML Data.

  1. You can embed inline XML Data within the tag inside XmlDataProvider Class.
  2. You can use Source to specify the Uri of the XML file.
  3. You can use Document property to specify the XmlDocument as binding source.
One Important property of XmlDataProvider Class is XPath which defines the level binding source in XML file.

 Embedding Inline XML Data:


If you see the XAML code XML Data is embed with in tag. This XML file has root element as Authors and child element Author with two attributes name and age.

          So for accessing these XML element in ListBox, i specified the itemsource to AuthorNames, XPath=* means it will returns all the data in XML. We can use XPath to query against the XML data
XPath=*[@age>25] will returns the Authors whose age is greater than 25. In this way query against Data.

Accessing XML Data By Setting Source:

             I Have an XML File as Follows:

I am displaying these data in UI as follows
Accessing XML Data Using XmlDataProvider

Here is the XAML Code:

  In this example i am loading main root elements i combobox, based on the combobox selection ListView is loaded with the Model, Based on the selection in ListView then Brand Price details will be shown. 
Watch this Video explaining How to Bind Xml Data to ListBox in WPF


        I hope you got some basic details about accessing xml data in wpf and bind it to UI.

       Don't forget to Comment and share it for our friends if you like this post. Thank You.


Thursday, 15 August 2013

Data Binding in WPF with Example


In this article i will explain

Download SourceCode And Video Explaining Binding in WPF 


Data Binding in WPF:

       One of the WPF Powerful feature is it's Data Binding Concept. It is the process establishing connection between Application User Interface and Business Logic.  It requires a Binding Source and Target .

Data Binding Architecture in WPF
Establishing Binding in WPF

           Source provides the data, and Target displays the data. Data Binding Connects UI to Data. When Source Data values changed then UI is updated. This Updation depends upon the Different types of Data Binding Modes. These Data Binding Decreases the code when compare to general binding. It also enables Template Binding means we customize the data more precisely by specifying which property to bind to which control.

    So a Binding Consists of 4 major Components:
  • Source
  • Source Path
  • Target Depenedency Object.
  • Target Dependency Property.
   Here Target can be any Element or any accessible property that is derived from Dependency Object for example TextBox controls Text Property is an Dependency Property. If you see the object hierarchy in wpf almost all controls are derived from Dependency Object. When coming to Source, it can be any public property, CLR object, Ado.Net DataSet, XMLDataProvider, ObjectDataProvider etc.

wpf binding in diagramatic format explanation
Binding a TextBox Text Property to a TextBlock Text Property

 If you see the above image it gives you clear picture about data binding in wpf. Here Source is a TextBox, Text property of this textbox is bind to TextBlock Text Property. You can see the Code and  output below.

Element as Binding Source Example:

Output is

WPF Simple Binding example
WPF Simple binding

In the above example if you type any text in textbox then immeadiatly it is displayed in TextBlock. Upto now we saw How to bind an element to another element, Now Let's see how to bind properties to XAML elements.

   Now add a class to your project and name that as MobileShop which consists of three properties ShopOwner, Shop Number, and Address as below

Now add a new window to your project and drag three textboxes ,three labels for displaying above three properties. XAML code looks like below.

Now in the Constructor of the Window write below code

As you may see that we have created an instance of class MobileShop and assigned the data for properties.
After that we assigned the object to DataContext Property of the Window. Here DataContext will hold the Data. This property exist for almost all controls that derived from Dependency Object i.e Panel Controls like Grid, Canvas, StackPanel, Wrappanel, Content Controls like Label, Button  etc. If we set our object as DataContext to a higher level control then all child controls can inherit that object. Read More About DataContext in WPF.

      So now in our present example we set object as DataContext for window parent control  so all the child controls have default data source, so we don't need to set Source we just need to mention the Path to which it binds. So in our example we only mentioned Path as below.

The same example can also be done without writing single peace of line code in code behind file, by writing only XAML code as below

Here first we have to set the namespace reference to access class files in the same project. i.e


Now we can access all contents in the project using local reference like below.

Binding in Code behind file:

 We can also perform data binding in code file. 
  • Create the Binding Object
  • Set the Binding Source
  • Set the path
  • Bind the object to Target using SetBinding Method.
Here is an example on how to perform binding in code behind file.

Binding Modes in WPF:

       Mode is the one property of the binding element which determines the flow of data between binding source and target. Four Binding Modes available.
  1. OneWay.
  2. OneTime.
  3. OneWayToSource.
  4. TwoWay.
OneWay:  If Mode is OneWay means Data flows from source to target whenever source is changed.

OneWay Binding Mode Image
OneWay Binding Mode
Here Target UI will be updated only if the source changed. Use this to display read only data in the UI.

OneTime:  If the Mode is OneTime then data flows from source to target when application starts or datacontext changes.
OneTime Binding Mode
OneTime Binding Mode
   You can use this to show read only data. This can be used to compare the changes occurred in UI. This Mode very useful source doesn't implement INotifyPropertyChanged Interface.

OneWayToSource: This is completely opposite to OneWay mode. In this Mode if target UI value is changed by user then it is updated in the source if the source is dependency property or it is implemented by INotifyPropertyChanged Interface.
OneWayToSource Binding
OneWayToSource Binding Mode
   In this case target is not updated if source is updated because of changes occurred in target.

TwoWay:  If the Mode is TwoWay then Whenever the Source is updated then target is updated, in the same manner if the target value is changed by user then Source is updated with target value and If the binding Source is bind to multiple targets  then all the targets are updated with Source data.

TwoWay Binding Example
TwoWay Binding Mode
          Use this Mode when User can change binding source and all it's targets.
For Understanding Binding Modes  briefly read this Binding Modes in WPF with example

 We have seen Binding Source is updated in OneWayToSource and TwoWay Binding Modes. More Precisely we can control the binding source updation by using UpdateSourceTrigger.

UpdateSourceTrigger in WPF:

          UpdateSourceTrigger is one of the property of Binding class. This Property defines the timing of binding source updation. This Property has three Options available. These are  LostFocus, PropertyChanged, Explicit. 

                Suppose you bind a property to a textbox, then if UpdateSourceTrigger is set to LostFocus then all the changes occurred in the textbox is updated in the source when TextBox Lost focus. If UpdateSourceTrigger is set to PropertyChanged then binding source is updated for every change in target. and if the UpdateSourceTrigger is Explicit then binding source is updated only when we explicitly called UpdateSource ( ) method.


               I hope now you will able to work with wpf bindings. Download the Source Code and watch the Video about Bindings in WPF. Let me know if you need any clarification.

             Don't forget to Comment and share for our friends if you like this post.

Wednesday, 14 August 2013

INotifyPropertyChanged Interface in WPF with Example


In this article i will explain how to Create INotifyPropertyChanged Interface in Class file, How to Implement it for Binding in wpf.

Download SourceCode


         INotifyPropertyChanged Interface defined in System.ComponentModel Class. It has an event Handler called PropertyChangedEventHandler which can be used to create an Event that can be used to notify the changes occured in Properties.

For example i had a class called MobileStore with Four Properties i.e Brand, Mode, Stock, Price. We can implement INotifyPropertyChanged interface for that class as follows.

Using INotifyPropertyChanged in Binding :

      In WPF Binding Target must be Dependency Property and Source can be Dependency Property or an CLR object or other. Here Dependency Property has built-in Notification feature. So if the Source is an CLR Object or other then we need take care about changes notification to target. In that case we have to use INotifyPropertyChanged Interface to notify the bindings that source property is changed you need to update the UI with new data.

Here is an Example using Above MobileShop class and its properties. The four Properties of MobileShop class are bind to textboxes.

XAML Code:


              I hope you got simple knowledge about How to implement INotifyPropertyChanged Interface for a Class and How to use it wpf binding.

          Don't forget to Comment and Share if you like this post for our friends. Thank You.

DataContext in WPF with Example


     In this article i will explain 
  • What is DataContext in WPF? and How to Bind Objects to DataContext.
  •  How DataContext helpful to WPF binding Techniques.



          DataContext is the Dependency Property which holds the Data. Here Data can be any type, it can be an CLR Object, a DataSet, XMLDataProvider, Any Collection Object etc. Almost all controls in wpf user interfaces are derived from Dependency Object so either Panel controls or Content Controls can have this Property.

             This Dependency Property has "Value Inheritance " feature means if you set Data Context as Data Object on Parent element then all of its descendent elements or children elements can reference to Data assigned to parent DataContext. i.e  All the child elements are bind against to the Data Object of parent control DataContext unless if we specify to bind against to other Objects.

            If DataContext is set on parent elements then no need to set binding source to children elements , we need to specify the path only for children elements. Let us see with example.

I have class called MobileShop with three properties.

Now i will show these three properties in three textboxes by setting MobileShop Object as DataContext for Window

Here is the XAML Code How the Window Looks like and How Child Elements access the DataContext of the Parent Control.

If you see the above xaml code for accessing the local class file in XAML code first i created a namespace reference to local as below

Now we can able to access project resources using local reference in XAML code like below

In the above lines i created an instance of MobileShop Class added data to properties and assigned that object as DataContext for Window Element. So for all the child elements we have to set the path only no need to set source again. 

Source Vs DataContext:

       There is a situation where parent element is set to DataContext object and Child Element bind to Source of another Object then Child Element binds to Source Object Only. Here child element checks first if any Source or RelativeSource bind to any object, if set  it will bind to that object, if Source or RelativeSource is not set then WPF Searches up in the element tree for DataContext of the Parent elements if it finds any non null Object then that objects used for Binding. This type of Binding use full when multiple properties are bind to Single Source.

Here is one example using same MobileShop class , where we will create two objects for this class and one object will bind to parent element as earlier, another bind to child element directly as source.


        In this way DataContext Property is useful when setting multiple properties to Single Source.

                        If you are binding single property then it is better to set as source for the element because of easy debugging i.e all the binding information exist in one place rather than searching up in the element tree for nearest DataContext. This DataContext defined in FrameworkElement Class.

      Don't forget to Comment and Share to our friends if you like this post. Thank You. 

WPF Tutorials

WPF Tutorials and How to Examples

Hi, this page contains the WPF Tutorials and How to examples Concept wise, i will update the WPF tutorials whenever updated.If you are new to WPF you can go through them in sequential order so that you can learn wpf from basics.

WPF Tutorials- Windows Presentation Foundation

 WPF Basics:

XAML Basics:

Data Binding in WPF:

MVVM Model(Model -View-View-Model):

Layout Controls in WPF:

  • Overview of WPF Layout Controls.
  • Grid Layout Control in WPF. 
  • Canvas Layout Control in WPF.
  • StackPanel Layout Control in WPF.
  • DockPanel Layout Control in WPF.
  • WrapPanel Layout Control in WPF.

WPF DataBindings:

  • Different Types of DataBinding Mode in WPF.
  • AttachedProperty in WPF.
  • Dependency Property in WPF.
  •  INotifyPropertyChanged Interface in WPF.
  • ValueConverters in WPF.
  • DataContext Property in WPF.
  • UpdateSourceTrigger in WPF.

Controls in WPF:

  • ListView Control in WPF and Usage.
  • DataGrid Control in WPF and Usage.
  • MediaElement Control in WPF and Usage.

Brushes in WPF:

  • SolidColorBrush in WPF and Usage.
  • LinearGradientBrush in WPF and Usage.
  • RadialGradientBrush in WPF and Usage.
  • ImageBrush in WPF and Usage. 

Transformations in WPF:

  • Rotate Transformation in WPF
  • Skew Transformation in WPF
  • Scale Transformation in WPF.
  • Translate Transform.

2D Drawings and Shapes in WPF:

How to Examples

Routed Events in WPF:

 Triggers in WPF:

  • Event Triggers in WPF
  • Property Triggers in WPF
  • Data Triggers in WPF

Styles and Resources in WPF:

Animations in WPF:

WPF Interoperability:

WPF UserControl:

Speech Synthesis in WPF:

Keep visiting this page i will update it everyday with new posts. Thank You.

UpdateSourceTrigger Property in WPF Binding Example


          In this article i will explain
  • What is the purpose of UpdateSourceTrigger Property,
  •  How to use UpdateSourceTrigger Property in WPF Binding, 
  • What are the different Options available for UpdateSourceTrigger Property etc.
  Download Source Code And Video Explanation of UpdateSourceTriggerUsage


             UpdateSourceTrigger is the one of the Property of Binding element in WPF. This Property defines the timing of binding source updates. There are two binding modes which updates the source i.e OneWayToSource and TwoWay.
UpdateSourceTrigger Property Required Modes

        So in this Two Modes we need specify when should occur the binding source updation. 

UpdateSourceTrigger Property has three properties.
  • LostFocus
  • PropertyChanged
  • Explicit. 
To understand this with example, add new window to wpf project and add new window which contains  a ListBox with List of colors and textbox whose Text, Background properties are bind to ListBox.SelectedItem.Content Property.

XAML Code is:

LostFocus:  If set, Binding Source will be updated when target control lost foucs. Now run the above example type one color in textbox and press tab you will notice that listbox is changed and Textbox background is changed.

PropertyChanged: If UpdateSourceTrigger Property is set to PropertyChanged, then source is updated with target data whenever change occurs in Target. Now Change UpdateSourceTrigger Property in our example to PropertyChanged. Now run the example and type in textbox, then ListBox is updated with this content.

Explicit:  If UpdateSourceTrigger Property is set to Explicit, then Binding source is updated only when BindingExpression.UpdateSource( ) method is called externally. Now in our example set the UpdateSourceTrigger Property to Explicitly and add a button to window. Now in button click write below code.

Now run our example and type one color in textbox and then click the button. Now the ListBox is updated with this data instead updating while typing or lost focus.

Default: Instead of these three options, Default option exist which will take place when we didn't specify any option in UpdateSourceTrigger Property. This Default option takes any of three options based on the Dependency Property of Target Control. For example Text Property of TextBox control has default option is LostFocus.

For More Graphical explanation Watch the Video Explaining the UpdateSourceTrigger Property


       I hope you are clear about How to use UpdateSourceTrigger Property in wpf bindings.

           Don't Forget to Comment and Share for our friends if you like this post. Thank You.

Binding Modes in WPF with Example


     In this article will see purpose of Mode property in Data Binding, What are the different Data Binding Modes available in WPF, When to use each of these binding modes etc.


       Mode is the one of the property of the Binding Element. Mode attribute defines the Data Binding Mode which determines how data flows between source and target. 

Four Types of Data Binding Modes available.
  • OneWay
  • OneTime
  • OneWayToSource
  • TwoWay

Thursday, 8 August 2013

Introduction to Object Oriented Programming(oops) Concepts in C#.Net With Examples

Object Oriented Programming Concepts in C#.Net:


This page contains list of  Object Oriented Programming Concepts i have written in this blog. You can find other Pages About .Net Technologies like WPF,Asp.Net, C#.Net,Vb.Net and Ado.Net.

   All the tutorials are given with examples. Go through it you will find enough information. This page is sitemap for oops concepts in this site.


Access Specifiers in C#.Net

This Article explains what are the access specifiers available in C#.Net and How to use them with example.

Access Specifiers in C#.Net in Diagrammatic Format

This Article gives you clear picture Access Modifiers for easily remembering hierarchy of usage of Access Specifiers.

Abstract Classes and Abstract Methods in C#.Net

This Oops Concept focuses on Abstract Types in C#.Net. Learn about how to create abstract class and Abstract methods and usage.

Static Classes and Static Class Members 

This Article focuses on creation of Static Classes in Oops Programming and How to Consume them in C#.Net

Sealed Classes and what is the use of them
This Article gives when to use Sealed Classes and what is their purpose.

Partial Class in C#.Net 
This Article gives brief explanation about How partial classes helps developers to work on one class at a time by multiple developers.

Interfaces in C#.Net

This Article focuses on purpose of interfaces and when to use interfaces.

How to identify the particular type of children control in a container control

Some times while programming we need to identify the particular type of controls and assign them certain settings in code behind file so in that case this article gives you how to catch the elements of container control in code behind file dynamically.

I will update this whenever i post any new articles about Oops Programming.
Keep Visiting Thank you.

How to add, read, clear the text content in richtextbox control in wpf

How to get, set, clear the text in richtextbox control in wpf


In this article you will know 
  • how to assign the text to richtextbox in wpf,
  •  how to read and clear the text from richtextbox, 
  • how to enable the spell check for richtextbox in wpf, 
  • what is the replacement text property for richtextbox,
  •  how to set the margins for richtextbox paragraphs. 

Wednesday, 7 August 2013

How to perform select, insert,update,delete operations using SqlCommand Object C#.Net and Vb.Net

Select,Insert,Update,Delete Operations on Database:


In this article you will learn how to use SqlCommand Object in Ado.Net for doing Select, Insert, Update, Delete operations against database.

              You will also learn different overloading methods for instantiation of SqlCommand Object, How to set CommandText as insert, update,delete sql statements,  what are the different methods available for execution of queries against database, How to use ExecuteScalar( ), ExecuteReader( ), ExecuteNonQuery( ) methods.

Tuesday, 6 August 2013

How to set connectionstring for SqlConnection Object to connect to sql server database with example

Setting Connection String for Sql Server Database


In this article you will learn about
  • how to set connection string parameter in SqlConnection Object, 
  • how to instantiate the SqlConnection, 
  • How to open the connection, 
  • How to pass the connection object to other ADO.NET objects, 
  • How it perform operations with other Ado.Net Objects,
  • How to close the connection.

      You can also find Introduction to Ado.Net Part1 and Part2 to know briefly about Ado.Net Data Providers.


Connection string parameters for Sql database and Oracle database with Example

Connection string parameters for SqlConnection ,Oralce Connection objects to Connect with Sql Server and Oracle databases


In this article you will learn what are the different connection string parameters need to set while connecting with either Sql Database or Oracle database.


In my previous article Introduction to Ado.Net part1 i mention that Creating connection is the first step while connecting with data sources.
         So to establish connection with Sql Server database we use SqlConnection object in the same manner OracleConnection object to connect with Oracle database.

Ado.Net Tutorial: Introduction to Ado.Net- Part2 Sending SqlCommand object and Getting Result using DataReader.

Introduction to Ado.Net:


This is Second Part of Ado.Net Introduction part1. In the first part i explained Basics of Ado.Net and How to Establish Connection With Database.

               Now in this part we will see how to send request using Sql Command Object and How to Get the Result from Database using DataReader Object.


2). Sending Request as an Sql Statement:

Monday, 5 August 2013

Ado.Net Tutorials: Introduction to Ado.Net - Part1 Establishing Connection with DataBase

Introduction to ADO.NET:


If you are new to .Net Technologies, want to get job in .net then start learning basics oops concepts and then learn Ado.Net Tutorials.
                 This article will give you brief introduction to ADO.NET in .NET like
  • What is Ado.Net, 
  • What Ado.Net Provides you to communicate with data sources, 
  • What are the providers provided by Ado.Net to Communicate with Data Sources like Flat Files(Text Files,XML FIles etc), Databases(Oracle, Sql Server, DB2, Teradata, MySql, Ms Access, etc), 
  • Architecture of Ado.Net, Object Model of Ado.Net,
  •  How to perform CRUD operations on Databases etc things.