WPF window problem

Leon NG 101 Reputation points
2023-02-27T04:25:21.7233333+00:00

Hi there,

User's image

How can I design a window and it can contain some sub window. those sub window can be moved out and in.

I have no idea to do that. Please give me some help.

Best Regards,

Leon

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-02-27T06:58:55.0633333+00:00

    Hi,@Leon NG. Welcome Microsoft Q&A.

    If you want to nested Windows, this is not allowed. You could set UserControl to SubWindow instead of Window

    Take the following example.

    MainWindow.xaml:

     <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <ToolBar Grid.Row="0">
                    <ToolBar.Items>
                    <Button x:Name="AddSubWindow" Content=" AddSubWindow" Width="100" Height="50" Click="AddSubWindow_Click"/>
                    <Button x:Name="MoveOutSubWindow" Content=" AddSubWindow" Width="100" Height="50" Click="MoveOutSubWindow_Click" />
                </ToolBar.Items>
                </ToolBar>
            <Border Name="MainBorder" Grid.Row="1">
                <Grid x:Name="grid">
                    
                </Grid>
            </Border>
            </Grid>
    

    MainWindow.xaml.cs:

     SubWindow sw = new SubWindow();
            private void AddSubWindow_Click(object sender, RoutedEventArgs e)
            {
                grid.Children.Add (sw);
            }
    
            private void MoveOutSubWindow_Click(object sender, RoutedEventArgs e)
            {
                grid.Children.Remove(sw);
            }
    

    SubWindow.xaml:

    <Grid>
            <Label Content="sub Window"   FontSize="40" Name="label1" VerticalAlignment="Top" Width="254" />
    
        </Grid>
    

    The result:

    45


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.