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:
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.