Dynamic Window with child windows in WPF

XAML WPF Developer 36 Reputation points
2021-11-24T08:20:08.707+00:00

Hi,
How to implement dynamic window in wpf. I want to dynamically height width set of main parent window according to child window

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-11-25T03:31:12.18+00:00

    The following is an example of the parent window changing the size according to the size of the child window, you could try to refer to the following code.
    MainWindow.xaml:

    <Window x:Class="DynamicParentWindowBaseChildWindow.MainWindow"  
            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:local="clr-namespace:DynamicParentWindowBaseChildWindow"  
            mc:Ignorable="d"  
            Title="MainWindow" >  
        <Grid>  
            <Button Content="Open Child Window" Height="65" HorizontalAlignment="Left" Margin="89,171,0,0" Name="button1" VerticalAlignment="Top" Width="316" Click="button1_Click" />  
        </Grid>  
    </Window>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
      {  
        ChildWindow Cw= new ChildWindow();  
        public MainWindow()  
        {  
          InitializeComponent();  
          App.Current.MainWindow.SizeChanged+= UpdateSize;  
        }  
        private void UpdateSize(object sender, EventArgs e)  
        {  
          this.Width = Cw.ActualWidth;  
          this.Height = Cw.ActualHeight;  
          this.UpdateLayout();  
        }  
        private void button1_Click(object sender, RoutedEventArgs e)  
        {  
            
          Cw.ShowInTaskbar=false;  
          Cw.Show();  
          
        }  
      }  
    

    ChildWindow.xaml:

    <Window x:Class="DynamicParentWindowBaseChildWindow.ChildWindow"  
            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:local="clr-namespace:DynamicParentWindowBaseChildWindow"  
            mc:Ignorable="d"  
            Title="ChildWindow" Height="450" Width="800">  
        <Grid>  
            <Label Content="child Window" Height="28" HorizontalAlignment="Left" Margin="12,22,0,0" Name="label1" VerticalAlignment="Top" Width="254" />  
          
        </Grid>  
    </Window>  
    

    The result:
    152481-2.gif


    If the answer is the right solution, please click Accept Answer and kindly upvote it. If you have extra questions about this answer, please click Comment. 
    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.

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.