I get error when trying to open up multiple RADWindows that are in multiple UserControls

12 posts, 2 answers
  1. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 27 Feb 2010 Link to this post

    Here I'm conducting a test to see if I can have several UserControls that contain RADWindow to appear on my MainPage.

    test.xaml contains a RADWindow:
    <UserControl 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
        mc:Ignorable="d" 
        x:Class="UniqueBrochure.test" 
        d:DesignWidth="640" d:DesignHeight="480"
     
        <Grid x:Name="LayoutRoot"
            <telerikNavigation:RadWindow x:Name="window" Header="RadWindow" WindowStartupLocation="CenterOwner" Width="300" Height="300"/> 
        </Grid>  
         
     
    </UserControl> 
    test.cs contains window.show() method to show it.


    Test2.xaml is also the same with different size.

    Now on my MainPage, I placed two buttons to create these two UserControls and adds them to the LayoutRoot: (this is not production code, just a test)
            private void Button_Click(object sender, System.Windows.RoutedEventArgs e) 
            { 
                test myTest=new test(); 
                this.LayoutRoot.Children.Add(myTest); 
            } 
             
            private void Button_Click2(object sender, System.Windows.RoutedEventArgs e) 
            { 
     
                Test2 myTest2=new Test2(); 
                this.LayoutRoot.Children.Add(myTest2); 
           } 
     

    Now when I run the app, I can only have one window show up at a time. If I try to open the second one while the first one is open, i get the following error:
    --------------------------
    "value does not fall within the expected range"
    error details:
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.Collection_InsertValue[T](PresentationFrameworkCollection`1 collection, UInt32 index, CValue value)
       at MS.Internal.XcpImports.Collection_InsertDependencyObject[T](PresentationFrameworkCollection`1 collection, UInt32 index, DependencyObject value)
       at System.Windows.PresentationFrameworkCollection`1.InsertDependencyObject(Int32 index, DependencyObject value)
       at System.Windows.Controls.UIElementCollection.InsertInternal(Int32 index, UIElement value)
       at System.Windows.PresentationFrameworkCollection`1.Insert(Int32 index, T value)
       at Telerik.Windows.Controls.HACKS.AttachPopupToVisualTree(Popup popup)
       at Telerik.Windows.Controls.PopupManager.Open(Popup popup, Boolean attachToVisualRoot)
       at Telerik.Windows.Controls.PopupManager.OpenWindow(Popup popup, Boolean attachToVisualRoot)
       at Telerik.Windows.Controls.PopupManager.Open(Popup popup, PopupType type, Boolean attachToVisualRoot)
       at Telerik.Windows.Controls.PopupManager.Open(Popup popup, PopupType type)
       at Telerik.Windows.Controls.RadWindowPopup.WindowPopupSilverlightFactory.WindowPopupSilverlightImpl.Show()
       at Telerik.Windows.Controls.RadWindowPopup.WindowPopupSilverlightFactory.WindowPopupSilverlightImpl.OpenPopup()
       at Telerik.Windows.Controls.RadWindowPopup.WindowPopup.Open(Boolean isModal)
       at Telerik.Windows.Controls.RadWindow.ShowWindow(Boolean modal)
       at Telerik.Windows.Controls.RadWindow.Show()
       at UniqueBrochure.Test2.Test2_Loaded(Object sender, RoutedEventArgs e)
       at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)


    --------------------------
    Is there a way to have multiple Window show on MainPage that are in different UserControls rather than all being in one UserControl?
    Thanks!
    ..Ben


  2. Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 01 Mar 2010 Link to this post

    Hi Ben,

     This way of using the RadWindow control is not correct - you shouldn't place the RadWindow control into an UserControl and you should put unique x:Name to all of the Window instances (when opened they go to a common name scope and it causes an error when more than one window have the same x:Name).

    Hope this information is helpful.

    Kind regards,
    Miroslav Nedyalkov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 01 Mar 2010 Link to this post

    Miro, you're correct. It was a mistake on my part to name them the same. That was because I copied it from the first one and didn't pay attention to Name. Changing the Name solved the problem. This is awesome being able to open several windows at the same time.

    However, you brought a good point not to use it in UserControl. This was actually a question that I was going to bring up. In SL, when you're creating a ChildWindow, you get a template to select a ChildWindow and that's it. However, it seems to me you have to have container for RADWindow first. So could you please tell me how a RADWindow SHOULD be created?

    Thank you for the answer!
  5. Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 01 Mar 2010 Link to this post

    Hello Ben,

     There are couple of "correct ways" to use the RadWindow control and one of them is exactly as the ChildWindow should be used. Unfortunately we don't have such template like the one of the ChildWindow and because of that it becomes more complicated - you might create a UserControl and change the RootTag to telerikNavigation:RadWindow and go to the code-behind and change it to inherit from RadWindow instead from UserControl. 

    As this approach is not straight-forward we suggest another solution that should solve the task as well - to create UserControl that represents the content of the RadWindow and every time you need to open the Window you create a new instance of the RadWindow class, create a new instance of your UserControl and set it as Content of the window.

    I understand that both approaches doesn't look as straight-forward as the approach you are using, but if you put the RadWindow control in XAML we need to move it out of the VisualTree. As you can see this currently works correctly, but it is considered as bad practice.

    Hope this information is helpful.

    Kind regards,
    Miroslav Nedyalkov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 01 Mar 2010 Link to this post

    Miro, this is a great discussion that's taking me to the right solution for the whole app.

     There are couple of "correct ways" to use the RadWindow control and one of them is exactly as the ChildWindow should be used. Unfortunately we don't have such template like the one of the ChildWindow and because of that it becomes more complicated - you might create a UserControl and change the RootTag to telerikNavigation:RadWindow and go to the code-behind and change it to inherit from RadWindow instead from UserControl.
    I'm going to pass on this suggestion :-)

    As this approach is not straight-forward we suggest another solution that should solve the task as well - to create UserControl that represents the content of the RadWindow and every time you need to open the Window you create a new instance of the RadWindow class, create a new instance of your UserControl and set it as Content of the window.
    This is exactly what I was thinking of.

  7. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 01 Mar 2010 Link to this post

    I understand that both approaches doesn't look as straight-forward as the approach you are using, but if you put the RadWindow control in XAML we need to move it out of the VisualTree. As you can see this currently works correctly, but it is considered as bad practice.

    I agree and didn't like it myself, that's why I wanted to start a thread asking about this. However, why is it that in all the samples the RADWindow is created in XAML? Why don't you have some samples that based on MenuItem click or Hyperlink click, it creates the RadWindow and assigns it's content to an existing User Control? That's why I got confused!
  8. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 01 Mar 2010 Link to this post

    Ok Miro, I've made great progress (Thanks to the suggestion). Here is the code for each MenuItem Click
       private void RADMenu_Clicked(object sender, RoutedEventArgs e) 
       { 
           RadMenuItem MenuItem = sender as RadMenuItem; 
           string winName = MenuItem.Tag.ToString();    
           if (winName !=string.Empty) 
           { 
               string nameSpace = this.GetType().Namespace; 
               Type type = Type.GetType(string.Format("{0}.{1}", nameSpace, winName)); 
               UserControl uc = Activator.CreateInstance(type) as UserControl;      
     
               RadWindow RadWin = new RadWindow(); 
               RadWin.Name=winName
               RadWin.Content=uc
               RadWin.Width=uc.Width; 
               RadWin.Height=uc.Height; 
               RadWin.Header=winName
               RadWin.WindowStartupLocation=WindowStartupLocation.CenterScreen; 
               RadWin.Show();               
     
           } 
       } 
    It does the job. I get my Window with it's User Control. Now before I create a new uc and RADWindow, I need to check with the RADWindowManager to see if it already has a copy of a Window in it's list.
    How can I ask the WindowManager if a Window with winName already exists, so I will not recreate it again?

    Thanks!
  9. Answer
    Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 02 Mar 2010 Link to this post

    Hello Ben,

    You could use the following code:

    private static void ShowWindow(string winName)
    {
        if (winName != string.Empty && RadWindowManager.Current.GetWindows().Any(w => string.Compare(w.Name, winName) == 0))
        {
            string nameSpace = "RadWindowExample";
            Type type = Type.GetType(string.Format("{0}.{1}", nameSpace, winName));
            UserControl uc = Activator.CreateInstance(type) as UserControl;
     
            RadWindow RadWin = new RadWindow();
            RadWin.Name = winName;
            RadWin.Content = uc;
     
            // You don't need this code - by default the Silverlight layout system will take care of this.
            //RadWin.Width = uc.Width;
            //RadWin.Height = uc.Height;
     
            RadWin.Header = winName;
            RadWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            RadWin.Show();
        }
    }

    If you have further questions, don't hesitate to ask.

    Regards,
    Miroslav Nedyalkov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  10. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 02 Mar 2010 Link to this post

    Thank you Miro, I got close to it but I was trying to use "Contains" extmethod v.s. the lambda expression you used and couldn't get it to work. This should work. I appreciate your help.
    Miro, do you know a good source for learning LINQ?
  11. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 02 Mar 2010 Link to this post

    Miro, I added your condition code to mine, and somehow the block of code never gets executed. It seems the returned value
    RadWindowManager.Current.GetWindows().Any(w => string.Compare(w.Name, winName) == 0
    is not zero, even the first time. Did you actually run your code to see if it worked?
    here is my final code, and no RADWindow appears, but as soon as I remove your condition it works
            private void RADMenu_Clicked(object sender, RoutedEventArgs e) 
            { 
                RadMenuItem MenuItem = sender as RadMenuItem; 
                string winName = MenuItem.Tag.ToString(); 
                 
                if (winName != string.Empty && RadWindowManager.Current.GetWindows().Any(w => string.Compare(w.Name, winName) == 0)) 
                { 
                    string nameSpace = this.GetType().Namespace; 
                    Type type = Type.GetType(string.Format("{0}.{1}", nameSpace, winName)); 
                    UserControl uc = Activator.CreateInstance(type) as UserControl;      
     
                    RadWindow RadWin = new RadWindow(); 
                    RadWin.Name=winName
                    RadWin.Content=uc
                    RadWin.ResizeMode=ResizeMode.NoResize; 
                    RadWin.Header=winName
                    RadWin.WindowStartupLocation=WindowStartupLocation.CenterScreen; 
                    RadWin.Show();               
     
                } 
            } 
    Thanks!
  12. Answer
    Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 02 Mar 2010 Link to this post

    Hello Ben,

     I'm sorry, I should of send the project - somehow I deleted the exclamation mark in front of the expression (just before RadWindowManager). The correct code was:

    private static void ShowWindow(string winName)
    {
        if (winName != string.Empty && !RadWindowManager.Current.GetWindows().Any(w => string.Compare(w.Name, winName) == 0))
        {
            string nameSpace = "RadWindowExample";
            Type type = Type.GetType(string.Format("{0}.{1}", nameSpace, winName));
            UserControl uc = Activator.CreateInstance(type) as UserControl;
      
            RadWindow RadWin = new RadWindow();
            RadWin.Name = winName;
            RadWin.Content = uc;
      
            // You don't need this code - by default the Silverlight layout system will take care of this.
            //RadWin.Width = uc.Width;
            //RadWin.Height = uc.Height;
      
            RadWin.Header = winName;
            RadWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            RadWin.Show();
        }
    }

    I'm sorry for the inconvenience caused.

    Best wishes,
    Miroslav Nedyalkov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  13. Ben Hayat
    Ben Hayat avatar
    892 posts
    Member since:
    May 2008

    Posted 02 Mar 2010 Link to this post

    Miro, I was just running it through debugger and came up to the same conclusion by reversing the result of the compare.
    Thank you for speedy response!

    p.s. I think you should add your sample to the online Demo of RADWindows!
Back to Top
DevCraft banner