Generally speaking, data binding is the flow between UI elements and the data from our application.
We got multiple ways for data binding.
We have one way data binding and two way data binding, for example.
And we will get started with one way data binding and from there we will move on to two way data binding.
So here we got our code behind file of the main window example.
When I now start the application, you can see that we have two textbox elements for name and age.
Now let's create a class for our project.
Because so far when we use UI elements, we just created simple variables like here's a string, here's
an integer and all of that.
But in real world applications you want to make use of, well, C-sharp.
Object oriented programming.
So let's create a class for our name and age.
And a good class could be person or animal or pet or whatever, right?
Let's start with a person class.
So right click on your project.
Let's create a new folder.
We want to separate it just a bit, call it data, and then right click on it and add a new C-sharp
class.
And please go ahead and call that one person.
So you could literally think of anything that consists out of a name and an age, right?
But we will go ahead with a person.
We want to add two properties.
We want to have a string for the name and an integer for the age.
So write down prop, which is the short code for property and then hit tab top.
Now you can see that we have public int and then we can write down H Pretty cool.
Let's do that one more time.
Prop hit tab.
Now write down string and hit tab again one more time and then you can finally set the name.
You could switch the order if you like.
Right?
I'll stick with age and then name.
That's fine.
But you could, as I said, switch the order easily.
Great.
So here we got our class now, or let's call it our data.
Or to be even more specific, the data we want to bind to our user interface elements like those text
boxes in one way, data binding, we will bind our data, which could be our person to our user interface
elements.
In that way, our person, that data is our source, right?
So in one way, data binding we bind from source to our target.
And our target are the UI elements.
Now we just created a class, but we have no instance, so we don't have any instance yet.
I mean, this is just like a template, right?
Like a blueprint.
Now let's create a real object that we can use for data binding inside of our main window.
Let's create a new person.
Let's write down person, person equals to you.
Create a new person.
Let's initialize it with some default values.
We could say that the name, for example.
The age could be something like 30.
So now we have created a real object inside of our main window when we initialize it.
Here's our constructor.
So inside of that constructor we want to set the so-called data context.
So call this to point to our main window instance and then say this dot data context equals to person.
And that data context in WPF is the underlying data structure that provides the binding information
between the UI elements that we have right here and the data that they display.
As I said, great.
We will see that just in a second.
We just preparing our data right now.
Next up, we will move on to the binding part.
So basically we just set up a C sharp class, created an instance, and now we say, okay, the context
or the data that we are talking about inside of our main window is this person right here, that object.
Now switch over to the main window, Examle.
And in the next video we will see how to bind this data.