Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a new Xaml Windows application named Slide with some elements. I want to see list of this Slides instances in listbox element.
C#
List slides = new List();
public void BtnNewSlide_OnClick(object sender, RoutedEventArgs e)
{
    slides.Add(new Slide());
    SlidesList.Items.Clear();
    SlidesList.ItemsSource = slides.ToList();
}

but I get this error when click on BtnNewSlide:

"Window must be the root of the tree. Cannot add Window as a child of Visual."
So how Can I create listbox of xaml windows?
Posted
Comments
D V L 22-Aug-15 9:34am    
error say simple and clear.
"Window must be the root of the tree. Cannot add Window as a child of Visual."

you can use UserControl.
Member 11684013 22-Aug-15 10:32am    
Thank you, but how?
Afzaal Ahmad Zeeshan 23-Aug-15 22:52pm    
I was just being suspicious about the code, but now I am sure that the code doesn't work. The List is either generic or a third-party, plus the type name of the a ListView is a ListView not a List.

Error simply says that you are trying to add Window control type to control's child list.
I mean ,suppose created one xaml of type Window(class from which you are inheriting your xaml) and again adding other class of Window type in newly created visual tree.


In your code, I presume Slide class is of type Window and you are adding that object to List ,That is the reason you are getting error.
Try modifying Slide class as UserControl Type.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900