我有两个“用户控件”,每个用户控件都有不同的高度。我把这个用户控件放在同一个窗口中,但是在不同的时间,问题是如何根据用户控件的高度动态改变高度窗口。
第一个用户控件是:
<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>
主窗口:
<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>
在代码 MainWindow 后面:
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);
}