Posted by: Zeeshan Amjad | August 9, 2009

Data Binding in WPF using C#


Data binding is very interesting concept in WPF, but on net and MSDN most of the time we saw example in XAML. Here we are going to see the Data binding in C# code not in XAML.

In C# data binding is a multi step process when using with list view. In list view we have to create each and every columns individually and assign the data binding with them. Here is a sample code to demonstrate this concept.

  1:         GridViewColumn idCol = new GridViewColumn();
  2:         idCol.Width = 25;
  3:         idCol.Header = "Id";
  4:         System.Windows.Data.Binding dId = new System.Windows.Data.Binding("Id");
  5:         dId.Path = new PropertyPath("Id");
  6:         idCol.DisplayMemberBinding = dId;
  7:         gView.Columns.Add(idCol);

we are defining the path of the data binding with PropertyPath class. Here is a complete program to show the current running process with their id and thread count if they have any main window title.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: using System.Windows.Controls;
  5: using System.Windows.Data;
  6: using System.Diagnostics;
  7: 
  8: public class ProcessInfo
  9: {
 10:     public String Id
 11:     { get; set; }
 12: 
 13:     public String Title
 14:     { get; set; }
 15: 
 16:     public String Thread
 17:     { get; set; }
 18: }
 19: 
 20: public class MyWindow : Window
 21: {
 22:     public MyWindow()
 23:     {
 24:         Title = "Binding Example";
 25:         Width = 400;
 26:         Height = 300;
 27:         Background = Brushes.Wheat;
 28: 
 29:         ListView list = new ListView();
 30:         list.Margin = new Thickness(10);
 31: 
 32:         GridView gView = new GridView();
 33:         list.View = gView;
 34: 
 35:         Content = list;
 36: 
 37:         GridViewColumn idCol = new GridViewColumn();
 38:         idCol.Width = 25;
 39:         idCol.Header = "Id";
 40:         System.Windows.Data.Binding dId = new System.Windows.Data.Binding("Id");
 41:         dId.Path = new PropertyPath("Id");
 42:         idCol.DisplayMemberBinding = dId;
 43:         gView.Columns.Add(idCol);
 44: 
 45:         GridViewColumn titleCol = new GridViewColumn();
 46:         titleCol.Width = 275;
 47:         titleCol.Header = "Title";
 48:         System.Windows.Data.Binding dTitle = new System.Windows.Data.Binding("Title");
 49:         dTitle.Path = new PropertyPath("Title");
 50:         titleCol.DisplayMemberBinding = dTitle;
 51:         gView.Columns.Add(titleCol);
 52: 
 53:         GridViewColumn threadCol = new GridViewColumn();
 54:         threadCol.Width = 45;
 55:         threadCol.Header = "Threads";
 56:         System.Windows.Data.Binding dThread = new System.Windows.Data.Binding("Thread");
 57:         dThread.Path = new PropertyPath("Thread");
 58:         threadCol.DisplayMemberBinding = dThread;
 59:         gView.Columns.Add(threadCol);
 60: 
 61:         Process[] processes = Process.GetProcesses();
 62: 
 63:         foreach (Process proc in processes)
 64:         {
 65:             if (proc.MainWindowTitle.Length == 0)
 66:                 continue;
 67: 
 68:             ProcessInfo procInfo = new ProcessInfo();
 69:             procInfo.Id = proc.Id.ToString();
 70:             procInfo.Title = proc.MainWindowTitle;
 71:             procInfo.Thread = proc.Threads.Count.ToString();
 72: 
 73:             list.Items.Add(procInfo);
 74:         }
 75:     }
 76: }
 77: 
 78: public class Binding
 79: {
 80:     [STAThread]
 81:     public static void Main()
 82:     {
 83:         MyWindow win = new MyWindow();
 84: 
 85:         Application app = new Application();
 86:         app.Run(win);
 87:     }
 88: }
 89: 

Here is the output of this program.

Binding

Advertisements

Responses

  1. when ever u give solution then give complete solution like aspx code and c# code. mean to say whole code and define their relation.

    • It is in fact complte code. I wrote the complete working code of C#. This is client program not a web one, therefore there isn’t any aspx code in it. Please let me know what did you find missing in it?

      Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: