Page 1 of 1

[Unity] Trouble adding framework elements dynamically

Posted: 01 Jan 2015, 04:34
by Scherub
Hi,

I tried to add a ContentPresenter or a ContentControl dynamically at runtime. So I created a new ContentPresenter/Control, set the ContentTemplate and DataContext and added this newly created element to the root panel. Unfortunately I wasn't able to spot the newly added control even in an empty test project. So I tested certain other controls such as StackPanels, Buttons, etc. and only a few controls (StackPanel and TextBlock) appeared, while the TextBox and the Button don't appear.

The next things I tried was to give these controls a fixed size, set the alignment, set a background color, add a custom control template, etc. but nothing seems to work.

Has anyone a clue what I am doing wrong?


Oh, and Happy New Year everyone! :)

Re: [Unity] Trouble adding framework elements dynamically

Posted: 02 Jan 2015, 06:45
by Scherub
Okay, I figured it out. I just forgot to set the Content, instead I set the DataContext. (On a side note: In WPF I don't have to set the content in order for it to be displayed.)

But there still seems to be something odd when I do this in the OnPostInit() method of a UserControl that is added by XAML in the Main-XAML. Apparently the actual size doesn't get set, perhaps because the size of the root panel is still NAN and the ActualSize 0. So it is still not displayed as described in my first post. You can try it by just creating a button in code. But it works as mentioned with certain other controls.

Re: [Unity] Trouble adding framework elements dynamically

Posted: 05 Jan 2015, 16:51
by sfernandez
I answered the ticket you opened in our bugtracker about this issue.