5

この基本的なレイアウトのウィンドウがあります。

<Window x:Class="GridStuffs.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Click="TopButtonClick" VerticalAlignment="Stretch">Top</Button>
    <Button Grid.Row="1" Name="_bottomButton">Bottom</Button>
</Grid>

これは、2つのボタン「上」と「下」を表示しているだけで、それぞれがウィンドウ内で等しい垂直方向のスペースを占めています。

一番上のボタンをクリックすると、以下が実行されます。

private void TopButtonClick(object sender, RoutedEventArgs e)
{
    if (_bottomButton.Visibility == Visibility.Collapsed)
    {
        _bottomButton.Visibility = Visibility.Visible;
    }
    else
    {
        _bottomButton.Visibility = Visibility.Collapsed;
    }
}

...下のボタンの表示を折りたたみと表示の間で切り替えます。

私がしたいのは、下のボタンが折りたたまれたときに、上のボタンのサイズを変更してウィンドウ全体に表示することです。実際に起こっているのは、下のボタンが非表示になっていることですが、上のボタンは元のサイズを保持しています。

質問:下部のボタンが折りたたまれているときに上部のボタンを展開してウィンドウを埋めるには、どのようなwpf / xamlの魔法を使用する必要がありますか?

4

1 に答える 1

16

RowDefinition上をに設定しHeight="*"ます。これは、使用可能なすべてのスペースを占めることを意味し、秒RowDefinitionをに設定Height="Auto"すると、コンテンツを表示するために必要なだけのスペースを占めることを意味します。

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

2番目のボタンの高さが定義されておらず、グリッドの高さの50%である必要がある場合は、コンバーターを使用して、Button.Heightプロパティを親グリッドの高さの半分にバインドします。

<Button Height="{Binding ActualHeight, 
    RelativeSource={RelativeSource Grid}, 
    Converter={StaticResource HalfOfValueConverter}}" />

興味があれば、私は実際に私のブログMathConverterに投稿しました。これは、バインドされた値を持つあらゆる種類の数式に使用します。

<Button Height="{Binding ActualHeight, 
    RelativeSource={RelativeSource Grid}, 
    Converter={StaticResource MathConverter},
    ConverterParameter=@VALUE/2}" />
于 2012-08-09T14:24:34.983 に答える