Page view counter
Create Usercontrol and add children in Xaml
Last post 10-08-2008 12:13 PM by sladapter. 12 replies.
Sort Posts:
10-05-2008 7:03 AM
Create Usercontrol and add children in Xaml

Hi all,

I have created a user control which basicly is a StackPanel.
In the code behind I can add "children" to this usercontrol and the usercontrol will generate these children and show them on the canvas.
The children can be any kind of FrameworkElement like a Button, Rectangle, Ellipse or even a Grid with it's own children.

Now I would like to add the ability to create the children at design time so that in the Xaml it would look like this:

<custom:MyStackPanel>
  <custom:MyStackPanel.Children>
    <Button x:Name="button1" />
    <custom:MyOtherControl x:Name="myother" />
  </custom:MyStackPanel.Children>
</custom:MyStackPanel>
How can I do this?
Thanks!
Regards,
Marrik



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-05-2008 3:19 PM
Marked as Answer
Re: Create Usercontrol and add children in Xaml

 UserControl does not have Children collection. Only Panel has. 

You might want to create this MyStackPanel as custom control instead of UserControl.

Public class MyStackPanel :StackPanel

{

}

Then you can add any control to the MyStackPanel.Children collection just like you do to StackPanel.


sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,691
10-05-2008 3:52 PM
Re: Create Usercontrol and add children in Xaml

Unfortunally my control is a UserControl and I need this so I can't change it to a Panel.



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-07-2008 1:52 AM
Marked as Answer
Re: Create Usercontrol and add children in Xaml

Hi Marrik,

In your condition, you shall use FindName to get the control. For the details, please take a look at my reply on this thread.  

Best regards,

Jonathan

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

Jonathan Shen – MSFT

Loading...
Joined on 06-18-2007
Posts 440
10-07-2008 12:40 PM
Re: Create Usercontrol and add children in Xaml

Hi Jonathan,

Thanks for your reply but unfortunally I cannot use this for 2 reasons:

First of all when I place anything within the tags of my usercontrol and try to compile it the compiter states "illegal property". The build will succeed however at runtime I get the same erroro.

Second is that I do not know the names of the children or even the children do not have names. So FindName will never work. And I don't want to place a Canvas as the first child of my usercontrol anyway.



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-07-2008 2:29 PM
Re: Create Usercontrol and add children in Xaml

It might rule out some things (or figure out some things) if you could provide the code for your UserControl.  With that said, my I inquire why you are locked to using a UserControl?  This would really be a solution better handled by a custom control since that is really what you seem to be doing.

 A possible option, and forgive me for not being able to show code at this time, might be to make the stack panel that resides within your UserControl available via a DependencyProperty.  If you can do this, it should allow access directly to the StackPanel from the XAML so you can access its Children.

If this post answered your question, please mark as answered.

Todd Herman

Todd Herman

Loading...
Joined on 06-17-2008
Posts 46
10-07-2008 2:53 PM
Re: Create Usercontrol and add children in Xaml

Hi Todd,

Thank you for your reply. My code is very extensive at the moment but I will try to create a small example of what I want to do asap and post is here. But basicly it will be the same as my first post.

Can you elaborate on what you mean by creating a custom control? Also I don't understand what you mean by using a DependencyProperty.

Thanks!



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-07-2008 11:00 PM
Marked as Answer
Re: Create Usercontrol and add children in Xaml

Hi Marrik,

Marrik:
Second is that I do not know the names of the children or even the children do not have names. So FindName will never work. And I don't want to place a Canvas as the first child of my usercontrol anyway.

Not like Canvas,  UserControl has no child property.   So the eaiser way is to assign name property to all the controls, which are inside the UserControl.  Then use FindName to get the controls.

Best regards,

Jonathan

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

Jonathan Shen – MSFT

Loading...
Joined on 06-18-2007
Posts 440
10-08-2008 10:31 AM
Re: Create Usercontrol and add children in Xaml

Hi Jonathan,

Then the problem remains the same: as soon as I place any control inside my UserControl I get that error (Illegal property). I must be doing something wrong... :(



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-08-2008 10:42 AM
Re: Create Usercontrol and add children in Xaml

As promised, a simple example:

My usercontrol: 

<UserControl x:Class="SilverlightApplication3.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="Auto" Height="Auto">
    <Canvas>
        <StackPanel x:Name="MyUserControlStackPanel" Width="Auto" Height="Auto"></StackPanel>        
    </Canvas>
</UserControl>

 Page.Xaml:

<UserControl x:Class="SilverlightApplication3.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:custom="clr-namespace:SilverlightApplication3"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <custom:MyUserControl x:Name="MyUserControlOnPage" Width="Auto" Height="Auto">
            <!-- insert controls here -->
        </custom:MyUserControl>
    </Grid>
</UserControl>

 Now as soon as you place any control (like a Button) in MyUserControl then I get the error "Illegal Property: Button [Line: 16 Position: 29]".

 Any ideas?



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-08-2008 10:59 AM
Marked as Answer
Re: Create Usercontrol and add children in Xaml

You can not do that. Because MyUserControl is a UserControl, it does not have Children Collection property. You can only add multiple controls that way if the Control has Children Collection property such as Panel control. UserControl is inherited from Control, not from Panel.  Or you can add one control that way if the Control has public Content property (such as Button control) or public Child property (such as Border control). UserControl does not have public Content property, so you can not add even one control to it. That's what we have been telling you.

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,691
10-08-2008 12:04 PM
Re: Create Usercontrol and add children in Xaml
Ok, I see what you mean. I was merely looking for a way to do this with a UserControl... I guess nobody has done this (nor wanted it :)).

I have added a Class to my test project: 

1    namespace SilverlightApplication3
2    {
3        public class MyPanel : StackPanel
4        {
5            public MyPanel() 
6            {
7                this.Loaded += new RoutedEventHandler(MyPanel_Loaded);
8            }
9    
10           void MyPanel_Loaded(object sender, RoutedEventArgs e)
11           {
12               double totalHeight = 0;
13               foreach (UIElement element in this.Children)
14               {
15                   if (element.GetType() == typeof(Button))
16                   {
17                       ((Button)element).UpdateLayout();
18                       totalHeight += ((Button)element).ActualHeight;
19                   }
20               }
21               this.Height = totalHeight;
22           }
23       }
24   }
 
And now the Page.Xaml looks like: 

<UserControl x:Class="SilverlightApplication3.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SilverlightApplication3"
    Width="400" Height="300">
    <StackPanel Orientation="Vertical" x:Name="LayoutRoot" Background="Yellow">
        <custom:MyPanel x:Name="test" Width="Auto" Height="100" VerticalAlignment="Top">
            <custom:MyPanel.Children>
                <Button Content="Test" x:Name="button1"></Button>
            </custom:MyPanel.Children>
        </custom:MyPanel>
        <Button Content="Extra"></Button>
    </StackPanel>
</UserControl>

And this seems to be working.

Now I can check in the page loaded event which children are available.

Thanks for your help.

Regards,
Marrik

 



Live long and prosper!

Marrik

Loading...
Joined on 10-03-2008
The Netherlands
Posts 17
10-08-2008 12:13 PM
Re: Create Usercontrol and add children in Xaml

 Yes, that is the way to go. By doing this, you created a Custom Control instead of a UserControl.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,691
Microsoft Communities