如何根据用户控件更改 WPF 中的高度窗口



我有两个"用户控件",每个用户控件具有不同的高度。我把这个用户控件放在同一个窗口中,但是在不同的时间,问题是如何根据高度用户控件动态更改高度窗口。

第一个用户控件是:

<UserControl x:Class="UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="auto" Width="auto">
 <Grid  Height="auto" Width="360" .....  >
     <Grid VerticalAlignment="Center" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto" MinHeight="20.8"/>
                    <RowDefinition Height="Auto" MinHeight="20"/>
                    <RowDefinition Height="Auto" MinHeight="18.4"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto" MinHeight="20.8"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="33*"/>
                    <ColumnDefinition Width="17*"/>
                </Grid.ColumnDefinitions>
                <TextBlock  Text="xx" Grid.Column="1" Grid.Row="0" ......./>
                <TextBlock  Text="yyy" Grid.Column="1" Grid.Row="1" ...... />
                                                 ............
                                                 ............
    </Grid>
 </Grid>
</UserControl>

Secound UserControl 是:

<UserControl x:Class="UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="auto" Width="auto">
 <Grid  Height="auto" Width="360" .....  >
     <Grid VerticalAlignment="Center" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="33*"/>
                    <ColumnDefinition Width="17*"/>
                </Grid.ColumnDefinitions>
                <TextBlock  Text="xx" Grid.Column="1" Grid.Row="0" ......./>
                <TextBlock  Text="yyy" Grid.Column="1" Grid.Row="1" />
                                                 ............
    </Grid>
 </Grid>
</UserControl>

主窗口:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Class="MainWindow"
        Title="Home" Height="550"*** Width="700" WindowStartupLocation="CenterScreen" ....>
 <Grid Name="Move">
    <Button Content="xxx"   Height="28" TextBlock.FontSize="15" Name="btn1" Click="click1"/>
    <Button Content="yyy"  Grid.Row="1" Name="btn2" Click="click2"/>
 </Grid>
</Window>

在代码主窗口后面:

  private void click1(object sender, RoutedEventArgs e)
        {
            UserControl1 add = new UserControl1();
            Move.Children.Clear();
            Move.Children.Add(add);
        }
        private void click2(object sender, RoutedEventArgs e)
        {
            UserControl2 add = new UserControl2();
            Move.Children.Clear();
            Move.Children.Add(add);
        } 

您可以尝试在代码隐藏中执行以下操作:Application.Current.MainWindow.Height = add。高度;您是否尝试过在主窗口上放置高度自动?

问候

最新更新