如何根据UserControl更改WPF中的高度窗口?
我有两个“UserControl”,每个UserControl都有不同的高度。我把这个UserControl放在同一个窗口中,但在不同的时间,问题是如何根据height UserControl动态地更改高度窗口 第一个用户控件是:如何根据UserControl更改WPF中的高度窗口?,wpf,c#-4.0,user-controls,Wpf,C# 4.0,User Controls,我有两个“UserControl”,每个UserControl都有不同的高度。我把这个UserControl放在同一个窗口中,但在不同的时间,问题是如何根据height UserControl动态地更改高度窗口 第一个用户控件是: <UserControl x:Class="UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml
<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>
<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>
您是否可以尝试在代码隐藏中执行以下操作:Application.Current.MainWindow.Height=add.Height;你试过在主窗上自动调高吗
关于,您是否可以尝试在代码隐藏中执行以下操作:Application.Current.MainWindow.Height=add.Height;你们有并没有试过在主窗上放高度自动?谢谢你们的帮助,你们能写下答案吗
<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);
}