You have seen how you can create HTML tags right inside of this editor right here.
You simply create a tag and then you position it and all of that.
But we can also do that from C sharp.
So let me just reset my window.
I simply click on window and reset my entire layout so that I have access to the main window HTML dot
CSS code behind file.
Now pay attention.
I'm just removing my button grid right now and I just keep track of row three, column four content
to.
So I just keep that in mind.
Now I want to remove it so that it's entirely gone.
So in my graphical representation you can see the second button isn't there anymore.
Now let me go to my code behind file.
So go to main window then Main window.
Right here.
This is a constructor.
You may have noticed that already.
So it's not a method.
It's a constructor.
So once the main window itself, the view, let's call it view, will get created.
We are able to well perform some additional actions right inside of that window.
Let's say we could create a button, for example.
How about the button that we have just removed?
So in that way we simply create a button.
Now let's hover above that.
Let's explore the namespace.
It's called System Windows Controls button, and it's, well, the button representation that you can
basically see here for the A button now.
Let's call that my button.
And that's a new button.
I mean, we are in C sharp, object oriented programming language, so we always have to make sure that
we create an instance of an object, right?
Then we can set the my button dot content property and we can set the content to be and now pay attention.
It is basically like the property that we are setting right here.
So we take the button or class and then we have the content property.
It's exactly the same like the button class and the content property.
So I guess you can see the pattern here right now.
Let's set our position.
Therefore we can simply take our grid that's well inside of our main window.
We can call a method called set row.
Then we want to put our button into the row three.
And guess what?
We can do basically the same for grid dot set column.
We want to put our button in column four.
So now, finally, the only thing that we have to do, which is a little bit different, is we need
to attach it.
Now the only thing left is we have to grab our actual grid that you can see well, right here grid.
So I want to show you one way on how to find specific tags so we can set the name in that way.
We set the name property right here, so x name.
And if we specify a name here, let's say my grid, then we can simply search for my grid.
And by using the name property, we'll be able to get a reference to that specific tag.
So let's search for the grid.
Let's say grid, my grid equals to and then find name.
So we find the tag, any element, and then we have to cast it into a grid because if we take a look
at the return type of find name, you can see it returns an object.
So it could be a button, it could be, well, barely anything, right?
So once we found something with my grid, we want to convert it or cast it into the type that we are
expecting, which is a grid in that case.
And now finally, we can take that grid, which is actually the grid inside of our application called
children, and then add so that we can add our button to it.
Well, definitely it is a little bit more work than what we can do right here in Xaml, Right?
But if you need to create objects or elements like buttons and whatever dynamically and you have no
option to do that here in Xaml, for example.
So whenever you want to create an element dynamically, you could potentially do that from within your
C-sharp code, as you can see right here.
So now let's start our application.
And as you can see, our button is here again, it's back, right?
So we have created it, positioned it and attached it to our grid using C-sharp so we can use Xaml to
create elements in text.
But basically we could do exactly the same using C-sharp.
Great.
So now you have created your first user interface element using C-sharp in WPF.