Posted by: Zeeshan Amjad | August 30, 2009

Dependency Property using VC++


We can do WPF programming any .Net based language. It is not limited to C# or VB.Net.

Here is a VC++ code to create dependency property using VC++.

  1: #include <windows.h>
  2: 
  3: using namespace System;
  4: using namespace System::Windows;
  5: 
  6: public ref class Employee : public DependencyObject
  7: {
  8: private:
  9: 	static DependencyProperty^ EmpIdProperty;
 10: 	static DependencyProperty^ NameProperty;
 11: 	static DependencyProperty^ DepartmentProperty;
 12: 	static DependencyProperty^ CompanyProperty;
 13: 
 14: public:
 15: 	Employee()
 16: 	{
 17: 		EmpIdProperty = DependencyProperty::Register("EmpId", Int32::typeid, Employee::typeid);
 18: 
 19: 		NameProperty = DependencyProperty::Register("Name", String::typeid, Employee::typeid);
 20: 
 21: 		DepartmentProperty = DependencyProperty::Register("Department", String::typeid, Employee::typeid);
 22: 
 23: 		CompanyProperty = DependencyProperty::Register("Company", String::typeid, Employee::typeid);
 24: 	}
 25: 
 26: 	property int EmpId
 27: 	{
 28: 		int get() { return (Int32)GetValue(EmpIdProperty); }
 29: 		void set(int value) { SetValue(EmpIdProperty, value); }
 30: 	}
 31: 
 32: 	property String^ Name
 33: 	{
 34: 		String^ get() { return (String^)GetValue(NameProperty); }
 35: 		void set(String^ value)	{ SetValue(NameProperty, value); }
 36: 	}
 37: 
 38: 	property String^ Department
 39: 	{
 40: 		String^ get() { return (String^)GetValue(DepartmentProperty); }
 41: 		void set(String^ value) { SetValue(DepartmentProperty, value); }
 42: 	}
 43: 
 44: 	property String^ Company
 45: 	{
 46: 		String^ get() { return (String^)GetValue(CompanyProperty); }
 47: 		void set(String^ value) { SetValue(CompanyProperty, value); }
 48: 	}
 49: };
 50: 
 51: [STAThread]
 52: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 53: 				   LPSTR lpCmd, int nCmd)
 54: {
 55: 	Window^ win = gcnew Window();
 56: 	win->Title = "Dependency Property";
 57: 	win->Width = 400;
 58: 	win->Height = 300;
 59: 
 60: 	Employee^ emp = gcnew Employee();
 61: 
 62: 	emp->EmpId = 1001;
 63: 	emp->Name = "Bob Smith";
 64: 	emp->Department = "Contractor";
 65: 	emp->Company = "WPF In Corp";
 66: 
 67: 	String^ text = gcnew String("");
 68: 	text = text->Format(" Emp ID = {0}\n Name = {1}\n Department = {2}\n Company = {3}",	
 69: 		emp->EmpId, emp->Name, emp->Department, emp->Company);
 70: 
 71: 	win->Content = text;
 72: 
 73: 	Application^ app = gcnew Application();
 74: 	app->Run(win);
 75: }
 76: 

Here is the output of this program.

vcwpf_09

Advertisements

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: