WPF - containers, Content controls, and children ...
Posted: Tue Apr 04, 2017 11:44 am
Hi guys of the X# forum,
I would like to start a thread where we focus on the WPF controls, from the point of view of which are 'containers', meaning which controls can accept either one, or many other controls.
It may surprise you to find out that a 'Window' object can actually only contain a single object. So it has therefore to be a 'Content' control.
Check out what we see in the editor when we try and add two Grids to the Window :-
Yes, we can have only one Grid at the root level of the visual tree. Yes, we can of cause then nest Grids, but that is then the property of the Grid, and not the Window.
Having seen this we may then guess that a 'UserControl' will be similar, and also have a 'Content' property taking a single object. Lets try it and see :-
Yep! - we need only one main grid to hold all the other 'child' objects for the UC. Remember Grids have a collection of children (objects).
Now then, something I forgot to say in a previous thread - when it comes to the syntax of the 'Content' attribute in XAML script. The space between the opening and closing tags is in effect the 'Content', without actually specifying it as such.
To prove this lets see a XAML example which we then add to later with X# code. Check this out :-
We have added a 'GroupBox' control using XAML script - note the <Grid> and </Grid> lines.
Before going to the X# code I will manually add a couple of things in the XAML - note the 'Names' :-
Now what we are about to do is add a second 'GroupBox' which we can then experiment with - first the second GB :-
Now finally, we can assign the 'Content' property of both the 'GroupBox' and a push Button control with suitable objects - check this out below :-
So in code we would always see the 'Content' as a property name - BUT - in XAML script we often just enclose the object to be contained within suitable tags - no sight of the word 'Content'. At first this can be a little confusing to guys new to the Designer/Editor surface, wondering what is going on.
Enough for now I feel, I hope this post may have cleared up a few things for some guys wishing to know more about WPF, the design tools, and XAML script as well.
Cheers, and Best regards,
remember "Learning is fun",
Phil.
Wales, UK.
I would like to start a thread where we focus on the WPF controls, from the point of view of which are 'containers', meaning which controls can accept either one, or many other controls.
It may surprise you to find out that a 'Window' object can actually only contain a single object. So it has therefore to be a 'Content' control.
Check out what we see in the editor when we try and add two Grids to the Window :-
Yes, we can have only one Grid at the root level of the visual tree. Yes, we can of cause then nest Grids, but that is then the property of the Grid, and not the Window.
Having seen this we may then guess that a 'UserControl' will be similar, and also have a 'Content' property taking a single object. Lets try it and see :-
Yep! - we need only one main grid to hold all the other 'child' objects for the UC. Remember Grids have a collection of children (objects).
Now then, something I forgot to say in a previous thread - when it comes to the syntax of the 'Content' attribute in XAML script. The space between the opening and closing tags is in effect the 'Content', without actually specifying it as such.
To prove this lets see a XAML example which we then add to later with X# code. Check this out :-
We have added a 'GroupBox' control using XAML script - note the <Grid> and </Grid> lines.
Before going to the X# code I will manually add a couple of things in the XAML - note the 'Names' :-
Now what we are about to do is add a second 'GroupBox' which we can then experiment with - first the second GB :-
Now finally, we can assign the 'Content' property of both the 'GroupBox' and a push Button control with suitable objects - check this out below :-
So in code we would always see the 'Content' as a property name - BUT - in XAML script we often just enclose the object to be contained within suitable tags - no sight of the word 'Content'. At first this can be a little confusing to guys new to the Designer/Editor surface, wondering what is going on.
Enough for now I feel, I hope this post may have cleared up a few things for some guys wishing to know more about WPF, the design tools, and XAML script as well.
Cheers, and Best regards,
remember "Learning is fun",
Phil.
Wales, UK.