Skip to main content

Microsoft Silverlight

Answered Question Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0RSS Feed

(0)

Marrik
Marrik

Member

Member

6 points

17 Posts

Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Hi all!

I have created a custom contentcontrol which basicly is a button.
When I add this button to the xaml it all works fine:  

<UserControl x:Class="MyControls.MyButtonParent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:custom="clr-namespace:MyControls;assembly=MyControls" 
    Width="Auto" Height="Auto">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel x:Name="stackpanelAll" Width="Auto" Height="Auto" HorizontalAlignment="Left">
            <custom:MyButton Content="Hello world"></custom:MyButton>
        </StackPanel>
    </Grid>
</UserControl>
Now I want to do the same in the code behind:
1    MyButton button = new MyButton()
2    {
3      Height = 30,
4      Width = Double.NaN,
5      Content = "Hello2"
6    }
7    
8    LayoutRoot.Children.Add(button);
This works fine as well.
But now I want to do something differtent:
I have created a UserControl which has a Grid and inside this grid a StackPanel. 
The StackPanels height is set to Auto. I add this UserControl to the Page.Xaml.
Then in the code behind of the Page I try to create the button again and add it
to the StackPanel of my UserControl.
To do this I have added a method in my UserControl which basicly does nothing else than: 
1    private void AddElement(FrameworkElement element)
2    {
3      stackPanelContent.Children.Add(element);
4    }
But now the button will NOT show! When I debug in de AddElement method I 
can see that the ActualHeight of the button is 0.0. Tongue Tied 
I have tried to force the layout of the button by using button.UpdateLayout()
but still no luck.
Can anybody tell me how to create this control and be able to (pre)render it 
so that I can get it's (actual)height?
Thanks!
Regards,
Marrik
Ps: This is done in Silverlight 2 RC0.
 



Live long and prosper!

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Answered Question

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Hi,

 I tried in same way as you told but not able to Reproduce this.

I am Pasting my code.Let me know if i am  missing some thing?

Page.Xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:SilverlightApplication1"
             Width="400" Height="300"
             >
    <StackPanel x:Name="LayoutRoot" Background="White">

   <local:SilverlightControl1  x:Name="myctrl" MouseLeftButtonDown="myctrl_MouseLeftButtonDown"></local:SilverlightControl1>
    </StackPanel>
</UserControl>

Page.Xaml.cs

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
        public Page()
        {
                      InitializeComponent();  

   Button b = new Button();
            b.Content = "Heelo";
            myctrl.AddElement(b as FrameworkElement); }

        private void myctrl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Button b = new Button();
            b.Content = "Heelo";
            myctrl.AddElement(b as FrameworkElement);
        }

      }

//silverlight  Control1 .xaml

<UserControl x:Class="SilverlightApplication1.SilverlightControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="Auto"
    >
    <Grid x:Name="LayoutRoot" Height="Auto" Width="Auto" Background="AliceBlue" >
        <StackPanel x:Name="stackpanelAll" Width="Auto" Height="Auto" HorizontalAlignment="Left">
        
        </StackPanel>
    </Grid>
</UserControl>

//silverlight Control1.xaml.cs

namespace SilverlightApplication1
{
    public partial class SilverlightControl1 : UserControl
    {
        public SilverlightControl1()
        {
            InitializeComponent();
            
       
        }
         public void AddElement(FrameworkElement element)
   {
       stackpanelAll.Children.Add(element);
   }


    
    }


 

 

Mark as answer if this post answered your question.

Harsh Bardhan

Marrik
Marrik

Member

Member

6 points

17 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Hi! Thanks for your reply.

The problem is that in the code behind I do some calculations on the FrameworkElement that is added to the stackpanel.
At runtime the actualsize on adding is always 0.0. I am not able to test your code now but I am sure that the same will happen.



Live long and prosper!

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

 Hi,

In my code also initial Height is Zero as i have not Specified that.

 

Mark as answer if this post answered your question.

Harsh Bardhan

Marrik
Marrik

Member

Member

6 points

17 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

I have specified it (30) but it still appears as 0.0. Only after a "postback" (so to say) the value of the height is set to 30.



Live long and prosper!

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Hi,

After Rendering it take some time.

Before that it will show you 0 only.

You can use dispatcher timer here and Put a delay and after that you can check that..

Mark as answer if this post answered your question.

Harsh Bardhan

Marrik
Marrik

Member

Member

6 points

17 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

This can't be done either: I am adding some (own built) ContentControls to my usercontrol at runtime (right after the page load event).

So a delay at any point won't do any good since the moment I add the contentcontrols I do some calculations (for example: calculate the total needed height for my usercontrol). And this is done immediatly.



Live long and prosper!

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Hello, can you post more code? After you call button.UpdateLayout, the ActualWidth/Height should be ready...

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Marrik
Marrik

Member

Member

6 points

17 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Yes, I will try to post a complete sample application which has this behaviour later this evening.

But I am doing a button.UpdateLayout() and still both the Height and ActualHeight are 0.0. :(



Live long and prosper!

Marrik
Marrik

Member

Member

6 points

17 Posts

Re: Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0

Closing this question. I solved it by instead of creating a contentcontrol creating a usercontrol that derives from a StackPanel.
Inside this usercontrol I can do the things needed in the Page_Loaded event.

Thanks all.



Live long and prosper!
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities