none
Window must be the root of the tree. Cannot add Window as a child of Visual.

    Question

  • I am working on expression blend.when i navigate from window1 to imagecoural i receive an error"Window must be the root of the tree. Cannot add Window as a child of Visual."

    Window1.xaml

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="IC2011SDEG.Window1"
        x:Name="Window"
        Title="Window1"
        Width="1200" Height="700" Loaded="Load">
       
        <Canvas x:Name="LayoutRoot">
            <Canvas.Background>
                <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF000000"/>
                    <GradientStop Color="#FFFFFFFF" Offset="0.8"/>
                    <GradientStop Color="#FF000000" Offset="1"/>
                </LinearGradientBrush>
            </Canvas.Background>
           
            <Canvas x:Name="Cover" Width="1200" Height="700">
                <Canvas.Background>
                    <SolidColorBrush Color="#FFFFFF" Opacity="0.8"/>
                </Canvas.Background>
                <TextBlock Text="Click To Start" Foreground="Blue" FontFamily="Verdana" FontWeight="Bold" FontSize="48" Canvas.Left="100" Canvas.Top="170"/>
            </Canvas>
        </Canvas>
       
    </Window>

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    Window1.xaml.cs

    namespace IC2011SDEG
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
           
        {
            ImageAdvanceCarousel _imageAdvanceCarousel;
            public Window1()
            {
                this.InitializeComponent();
               

                // attache a opened handler to image
                _imageAdvanceCarousel = new ImageAdvanceCarousel();

                LayoutRoot .Children.Insert(0,_imageAdvanceCarousel);//Error:"Window must be the root of the tree. Cannot add Window as a child of Visual." //

                // click to remove the cover
                Cover.MouseLeftButtonDown += new MouseButtonEventHandler(Cover_MouseLeftButtonDown);
               
                // Insert code required on object creation below this point.
            }

            private void Load(object sender, System.Windows.RoutedEventArgs e)
            {
                // TODO: Add event handler implementation here.
            }
              void Cover_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                LayoutRoot.Children.Remove(Cover);
                _imageAdvanceCarousel.Start();
            }
    }
    }

    ImageAdvanceCoural.xaml

    <Window x:Class="IC2011SDEG.ImageAdvanceCarousel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="1200" Height="700">
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Canvas Width="1200" Height="400" x:Name="Holder" Background="Transparent"></Canvas>
           
            <StackPanel VerticalAlignment="Bottom" Orientation="Horizontal" Width="550" Margin="0 8">
                <Grid Width="138">
                    <Image Source="IMAGES/up.png" Width="48" Height="48" x:Name="MoveLeftButton" Cursor="Hand">
                        <Image.RenderTransform>
                            <RotateTransform Angle="-90" CenterX="24" CenterY="24"></RotateTransform>
                        </Image.RenderTransform>
                    </Image>
                </Grid>

                <Grid Width="138">
                    <Image Source="IMAGES/up.png" Width="48" Height="48" x:Name="DrillDownButton" Cursor="Hand"/>
                </Grid>
                <Grid Width="138">
                    <Image Source="IMAGES/down.png" Width="48" Height="48" x:Name="DrillUpButton" Cursor="Hand">
                    </Image>
                </Grid>
                <Grid Width="138">
                    <Image Source="IMAGES/up.png" Width="48" Height="48" x:Name="MoveRightButton" Cursor="Hand">
                        <Image.RenderTransform>
                            <RotateTransform Angle="90" CenterX="24" CenterY="24"></RotateTransform>
                        </Image.RenderTransform>
                    </Image>
                </Grid>
            </StackPanel>
           
          
        </Grid>
    </Window>

    ImageAdvanceCoural.xaml.cs

     

    namespace IC2011SDEG
    {
        public partial class ImageAdvanceCarousel : Window
        {
            List<ImageCarousel> _imageCarousels = new List<ImageCarousel>(); // Store the Added Carousels
            int _currentLayer = 0;                                           // Store the Layer index
            ImageCarousel _currentCarousel;                                  // Current Carousel

            public ImageAdvanceCarousel()
            {
                InitializeComponent();

                // Add the Button Handlers
                MoveLeftButton.MouseLeftButtonDown += new MouseButtonEventHandler(MoveLeftButton_MouseLeftButtonDown);
                MoveRightButton.MouseLeftButtonDown += new MouseButtonEventHandler(MoveRightButton_MouseLeftButtonDown);
                DrillDownButton.MouseLeftButtonDown += new MouseButtonEventHandler(DrillDownButton_MouseLeftButtonDown);
                DrillUpButton.MouseLeftButtonDown += new MouseButtonEventHandler(DrillUpButton_MouseLeftButtonDown);
            }


            /////////////////////////////////////////////////////       
            // Handlers
            /////////////////////////////////////////////////////   


            // Drill Up
            void DrillUpButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                drillUp();
            }

            // Drill Down
            void DrillDownButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                drillDown();
            }

            // Move the Current Carousel to Right
            void MoveRightButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                if (_currentCarousel != null)
                {
                    _currentCarousel.MoveRight();
                }
            }

            // Move the Current Carousel to Left
            void MoveLeftButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {

                if (_currentCarousel != null)
                {
                    _currentCarousel.MoveLeft();
                }
            }

            /////////////////////////////////////////////////////       
            // Private Methods
            /////////////////////////////////////////////////////   

            private void drillDown(){
                _currentLayer ++;

                // create new layer if not exist
                if (_currentLayer > _imageCarousels.Count)
                {
                    _currentCarousel= new ImageCarousel();
                    _imageCarousels.Add(_currentCarousel);
                    Holder.Children.Add(_currentCarousel);
                    _currentCarousel.Start();
                    _currentCarousel.DrillAppear();
                }
                else
                {
                    // get the current layer if exist
                    _currentCarousel = _imageCarousels[_currentLayer - 1];
                    _currentCarousel.DrillAppear();
                }

                // Drill down the next layer (need to check if it exist)
                if (_currentLayer - 2 >= 0)
                {
                    ImageCarousel imageCarousel = _imageCarousels[_currentLayer - 2];
                    imageCarousel.DrillDown();
                }
            }

            // Drill up the current layer
            private void drillUp()
            {
                if (_currentLayer > 1)
                {
                    _currentLayer--;
                    _currentCarousel = _imageCarousels[_currentLayer - 1];
                    _currentCarousel.DrillUp();

                    // remove the last layer
                    if (_currentLayer + 1 <= _imageCarousels.Count)
                    {
                        ImageCarousel imageCarousel = _imageCarousels[_currentLayer ];
                        imageCarousel.DrillDisappear();
                    }
                }
            }

            /////////////////////////////////////////////////////       
            // Public Methods
            /////////////////////////////////////////////////////   

            // Start by drill the first layer
            public void Start()
            {
                drillDown();
            }
        }
    }

    Sunday, November 28, 2010 7:09 PM

Answers

  • All you need to do is change the parent class of ImageAdvanceCarousel to be UserControl instead of Window.  You can't have nested windows, which is what you are doing.  Make sure you update the XAML as well, so that the root of the XAML is UserControl instead of Window.
    Monday, November 29, 2010 8:29 PM
  • Hi Srithar,

    Welcome to our forum.

    Base on your description I think the root cause of your issue is you try to add a "Window" to your "Grid" and "Canvas", it is not allowed.

    Solution 1:

    you can try to new a Window and display it.

    Solution 2:

    you can define your "ImageAdvanceCoural" as a UserControl and resource, then you can add it in your first window.

    If anything is unclear about how to do it, please let me know.

     

    Best regards,

    Sheldon _Xiao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Thursday, December 02, 2010 5:00 AM

All replies

  • All you need to do is change the parent class of ImageAdvanceCarousel to be UserControl instead of Window.  You can't have nested windows, which is what you are doing.  Make sure you update the XAML as well, so that the root of the XAML is UserControl instead of Window.
    Monday, November 29, 2010 8:29 PM
  • Hi Srithar,

    Welcome to our forum.

    Base on your description I think the root cause of your issue is you try to add a "Window" to your "Grid" and "Canvas", it is not allowed.

    Solution 1:

    you can try to new a Window and display it.

    Solution 2:

    you can define your "ImageAdvanceCoural" as a UserControl and resource, then you can add it in your first window.

    If anything is unclear about how to do it, please let me know.

     

    Best regards,

    Sheldon _Xiao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Thursday, December 02, 2010 5:00 AM
  • Hi Srithar,

    I am marking your issue as "Answered", if you have new findings about this issue, please unmark.

     

    Best regards,

    Shedon _Xiao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Monday, December 06, 2010 1:47 AM