user3342256
user3342256

Reputation: 1290

Dynamically Expanding XAML Window

I am trying to get my XAML window to automatically expand in height once a checkbox is checked. The checkbox triggers grid "OutputGrid" to be uncollapsed. I can't find the right combination of syntax to achieve a relatively "tight/collapsed" look that automatically expands upon new controls showing.

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>

Upvotes: 1

Views: 111

Answers (1)

walterlv
walterlv

Reputation: 2376

Window has a property named SizeToContent. It means that you can keep your Width or Height property unset and make the size flexible when content size changed.

SizeToContent property

<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>

Notice that I've removed the Height value.

Upvotes: 2

Related Questions