Binding WPF DataContext to Dictionary

Created one custom WPF control with a textbox. We can bind the custom WPF control to data in different ways.

Bind Using Class Object

Xaml

Code Behind

1. Create object of the class Address like
Address add=new Address(){Street=”Hyderabad Eat Street”};
2. Assign to DataContext of the user control
userControl11.DataContext = add;

Bind Using Dictionary

When binding using a dictionary object, we are binding to an element using the string key; so the key should be inside square brackets.

Xaml
<textbox height="23" margin="139,26,0,0" name="textBox1" width="157" text="{Binding [Street], Mode=TwoWay}”>

Code Behind

1. Create the Dictionary Object
Dictionary data = new Dictionary();
data.Add(“Street”, “Hyderabad Eat Street”);

2. Assign to DataContext of the user control
userControl11.DataContext = data;

Advertisements

About ambilykk

I am a Technology Evangelist on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Azure and Visual Studio. Technology adoption and learning is my key strength and technology sharing is my passion.
This entry was posted in WPF. Bookmark the permalink.

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