0
我有兩個「UserControl」,每個UserControl都有不同的高度。我把這個UserControl放在同一個窗口中,但是在不同的時間,問題是如何根據高度UserControl動態改變高度窗口。如何根據UserControl更改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>
謝勝利用戶控件是:
<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.Height; 你有沒有嘗試在主窗口把高度自動? – sexta13
謝謝你幫助我,你能寫出答案 – ITInWorld