我正在尝试使我的XAML窗口在选中一个复选框后自动在高度上扩展。复选框触发网格“ OutputGrid”被折叠。我找不到正确的语法组合来实现相对“紧缩/折叠”的外观,该外观会在显示新控件时自动扩展。
XAML:
<Window x:Class="Log_Grabber.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Log_Grabber"
mc:Ignorable="d"
Title="File Grabber" MinHeight="430" Height="392" MinWidth="575" Width="575">
<Grid Margin="5">
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="450" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Grid.Row="0" Margin="3 0 0 0" Text="File to Grab:"></TextBlock>
<TextBox Grid.Row="1" Name="FiletoGrabTextBox" Margin="3"></TextBox>
</StackPanel>
<Button Margin="0 0 0 3" Content="Browse" Width="Auto" Grid.Column="1" Height="18" VerticalAlignment="Bottom" VerticalContentAlignment="Center" Click="Browse_Click" ></Button>
</Grid>
<Separator></Separator>
<TextBlock Margin="3 0 0 0" Text="Computer Names:"></TextBlock>
<TextBox Name="ComputersNamesTextBox" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" Margin="3" Height="200"></TextBox>
<Separator></Separator>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="450" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Margin="3 0 0 5" Text="Destination:"></TextBlock>
<TextBox Name="DestinationTextBox" Margin="3"></TextBox>
</StackPanel>
<StackPanel Grid.Column="1">
<CheckBox Name="ShowOutputCheckBox" Checked="ShowOutputCheckBox_Checked" Unchecked="ShowOutputCheckBox_Unchecked" Content="Show Output" Margin="7 4 0 5"></CheckBox>
<Button Margin="0 0 0 3" Content="Browse" Width="Auto" Height="18" VerticalAlignment="Bottom" VerticalContentAlignment="Center" Click="Destination_Browse_Click" ></Button>
</StackPanel>
</Grid>
<Separator Margin="0 10 0 0"></Separator>
<Grid Name="OutputGrid" Visibility="Collapsed">
<StackPanel>
<Separator Margin="0 10 0 0"></Separator>
<ListBox Name="OutputListBox" Height="150" Margin="0 0 0 10" ScrollViewer.VerticalScrollBarVisibility="Auto" ></ListBox>
<Separator></Separator>
</StackPanel>
</Grid>
<Button Margin="0 10 0 0" Content="Grab 'em!" HorizontalAlignment="Center" Width="Auto" Click="GrabEm_Click"></Button>
<TextBlock Name="RunningAsTextBlock" Margin="0 10 0 0" HorizontalAlignment="Center"></TextBlock>
</StackPanel>
</Grid>
答案 0 :(得分:2)
Window
具有一个名为SizeToContent
的属性。这意味着您可以保留Width
或Height
属性的未设置状态,并在更改内容大小时使大小灵活。
<Window x:Class="Log_Grabber.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Log_Grabber"
mc:Ignorable="d"
Title="File Grabber" MinHeight="430" MinWidth="575" Width="575"
SizeToContent="Height">
</Window>
请注意,我已经删除了Height
值。