[Unity] Trouble adding framework elements dynamically
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!
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
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.
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.
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: [Unity] Trouble adding framework elements dynamically
I answered the ticket you opened in our bugtracker about this issue.
Who is online
Users browsing this forum: No registered users and 5 guests