none
Programmatically add a WPF UserControl to a WPF Window

    Question

  • Hey,

    I have a bunch of user controls for the different screens in my system.  When I need to show a screen, I want to create a new window instance, put the user control as the content of the window, and show the window.  When the window first shows, I want the window to be sized so that the user control is completely visible in it without any extra space (ie if the user control is 300 x 300 in the designer, the window perfectly shows the 300 x 300).  I also want to be able resize the window and have the user control resize with the window.

    Is this possible?

    The closest I was able to get to this is doing something like: store the user control's original height/width, set the user control's height/width to double.NaN, create the window and set the content of the window to the user control, then set the window's height and width to the user control's original height and width.  The problem with this is that the window's size was slightly too small to completely fit the user control.  Are the title bar/borders included in the window's height/width?  If so, is there a way to determine what effect they have?  Or, is there another way to do what I'm trying to do?


    Thanks
    Friday, July 11, 2008 12:47 AM

Answers

  • I dug around the forums some more with some different search criteria and found this thread:

    http://forums.msdn.microsoft.com/en/wpf/thread/59a458fe-a94e-4109-bb9c-690ad5785181/


    Here's the method I made based on the above post if anyone needs it (it works, but I don't completely understand why, so it may not be as good as it could be).

            /// <summary>  
            /// Creates a <see cref="Window"/> hosting <paramref name="userControlToHost"/>.  
            /// </summary>  
            /// <param name="userControlToHost">  
            /// The <see cref="UserControl"/> to host in the <see cref="Window"/>.  
            /// </param>  
            /// <returns>  
            /// The <see cref="Window"/> hosting <paramref name="userControlToHost"/>.  
            /// </returns>  
            public Window CreateWindowHostingUserControl(UserControl userControlToHost)  
            {  
                //Create a border with the initial height and width of the user control.  
                Border borderWithInitialDimensions = new Border();  
                  
                borderWithInitialDimensions.Height = userControlToHost.Height;  
                borderWithInitialDimensions.Width = userControlToHost.Width;  
     
     
                //Set the user control's dimensions to double.NaN so that it auto sizes  
                //to fill the window.  
                userControlToHost.Height = double.NaN;  
                userControlToHost.Width = double.NaN;  
     
     
                //Create a grid hosting both the border and the user control.  The   
                //border results in the grid and window (created below) having initial  
                //dimensions.  
                Grid hostGrid = new Grid();  
     
                hostGrid.Children.Add(borderWithInitialDimensions);  
                hostGrid.Children.Add(userControlToHost);  
     
     
                //Create a window that resizes to fit its content with the grid as its   
                //content.  
                Window hostWindow = new Window();  
     
                hostWindow.Content = hostGrid;  
                hostWindow.SizeToContent = SizeToContent.WidthAndHeight;  
     
     
                return hostWindow;  
            } 
    • Marked as answer by lotrij Friday, July 11, 2008 12:50 PM
    Friday, July 11, 2008 12:50 PM

All replies

  • Window has a property SizeToContent. Maybe that can help.

    hth,
    Marcel
    Friday, July 11, 2008 7:13 AM
  • That property works when the window is initially shown, but the user control doesn't resize with the window.  And if I set the user control's height and width to double.NaN to get the user control to resize with the window then the initial window has no height/width when it is shown.

    • Edited by lotrij Friday, July 11, 2008 12:20 PM More detail
    Friday, July 11, 2008 11:59 AM
  • I dug around the forums some more with some different search criteria and found this thread:

    http://forums.msdn.microsoft.com/en/wpf/thread/59a458fe-a94e-4109-bb9c-690ad5785181/


    Here's the method I made based on the above post if anyone needs it (it works, but I don't completely understand why, so it may not be as good as it could be).

            /// <summary>  
            /// Creates a <see cref="Window"/> hosting <paramref name="userControlToHost"/>.  
            /// </summary>  
            /// <param name="userControlToHost">  
            /// The <see cref="UserControl"/> to host in the <see cref="Window"/>.  
            /// </param>  
            /// <returns>  
            /// The <see cref="Window"/> hosting <paramref name="userControlToHost"/>.  
            /// </returns>  
            public Window CreateWindowHostingUserControl(UserControl userControlToHost)  
            {  
                //Create a border with the initial height and width of the user control.  
                Border borderWithInitialDimensions = new Border();  
                  
                borderWithInitialDimensions.Height = userControlToHost.Height;  
                borderWithInitialDimensions.Width = userControlToHost.Width;  
     
     
                //Set the user control's dimensions to double.NaN so that it auto sizes  
                //to fill the window.  
                userControlToHost.Height = double.NaN;  
                userControlToHost.Width = double.NaN;  
     
     
                //Create a grid hosting both the border and the user control.  The   
                //border results in the grid and window (created below) having initial  
                //dimensions.  
                Grid hostGrid = new Grid();  
     
                hostGrid.Children.Add(borderWithInitialDimensions);  
                hostGrid.Children.Add(userControlToHost);  
     
     
                //Create a window that resizes to fit its content with the grid as its   
                //content.  
                Window hostWindow = new Window();  
     
                hostWindow.Content = hostGrid;  
                hostWindow.SizeToContent = SizeToContent.WidthAndHeight;  
     
     
                return hostWindow;  
            } 
    • Marked as answer by lotrij Friday, July 11, 2008 12:50 PM
    Friday, July 11, 2008 12:50 PM