Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Creating a custom contentcontrol at runtime gives an ActualHeight of 0.0
9 replies. Latest Post by Marrik on October 12, 2008.
(0)
Marrik
Member
6 points
17 Posts
10-03-2008 8:46 AM |
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. 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.
HarshBar...
Star
9908 points
1,719 Posts
10-03-2008 11:11 AM |
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); }
10-03-2008 11:34 AM |
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.
10-03-2008 11:53 AM |
In my code also initial Height is Zero as i have not Specified that.
10-03-2008 12:00 PM |
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.
10-06-2008 5:11 AM |
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..
10-06-2008 5:19 AM |
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.
Yi-Lun L...
All-Star
25052 points
2,747 Posts
10-07-2008 1:16 AM |
Hello, can you post more code? After you call button.UpdateLayout, the ActualWidth/Height should be ready...
10-07-2008 12:34 PM |
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. :(
10-12-2008 7:52 AM |
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.