我有两个“UserControl”,每个UserControl都有不同的高度。我把这个UserControl放在同一个窗口,但是在不同的时间,问题是如何根据UserControl的高度动态改变高度窗口。
First UserControl是:
<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>
在幕后代码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);
}
答案 0 :(得分:0)
你可以在后面的代码中尝试做类似的事情:Application.Current.MainWindow.Height = add.Height;你有没有试过在主窗口把高度自动?
的问候,