Learn Binding Keyword in XAML and C#
- Usama Liaquat
- Dec 6, 2016
- 2 min read
Today!
I am gonna show you how to use Binding in our project. So in this post that's a simple binding contain "FirstName" and the "LastName" functions.
Create a new Project by pressing the Ctrl + Shift + N.
As you can see. The projects pannel window open. So in here expand visual C# > Windows > Windows8 > select Windows select the Blank App and create a new Project.

When the project is created you can see in the solution explorer. Now create a new class by right click to the Solution > Add > Class and name them "anyperson.cs".

and click to add. Now may be solution explorer look like this :

Now Open "anyperson.cs" In here you can see this type of text

Now the class have no access modifier just type public keyword like this

In these two braces {} type in here these functions using the get and set. So this must be in a public modifiers because i am calling these functions in "MainPage.xaml.cs".
So this is the simple example of data binding.
I am using two function 1st is "FirstName" and the 2nd is "LastName". Something like this.

Now go to "MainPage.xaml.cs" using the solution explorer. Maybe its look like this :

Now type it here the modifier " Public" and call the class " anyperson" and name them "anyword" and set the value of "FirstName" and the "LastName". It look like this.

Now its time to create a view using Xaml. Just double click to the "MainPage.xaml" in the solution explorer. May be its look like this

Now go to xaml and type it here <stackpanel> </stackpanel> its look like this

In this <stackpanel> again create a <stackpanel> this time change Orientation of stackpanel is Horizontal. Something like this

In this stackpanel type two textblock. On the second textblock of the text property binded to the FirstName. its like this . .

Now type Second Stackpanel to show the next function. Look like this

Now Go to the "MainPage.xaml.cs". In here type OnNavigatedto Method. look like this

Now type in here DataContent = Usama; /* This is only the name i am created on anyperson*/ look like...

Now debug our app by pressing F5. The output its look like this


its works ! So now the declarative binding statement < Object Property = "{Binding bindingArgs}" />
and you can use #FallbackValue || #TargetNullValue
The BindingArgs is a set to name , value , pairs that further refine the binding. There are a lot of more actions to explore them. This is the simple example what the binding can do in Xaml + C#
Thanks to see this post
Hope you guyz enjoy!
Note: If you have any question comment down here. And i will reply your comments with in a minutes or may be an hour.
Watch Video!
Comments