WPF网格设置为窗口的ActualWidth,发生剪切

时间:2015-08-28 21:51:10

标签: wpf wpf-controls

我在这里找到了一个答案,关于如何确保在调整Window的大小时,其内部的网格也会相应调整大小。我面临的问题是我在网格内部有一个StackPanel,以及" X" (关闭)按钮未完全显示(只有大约一半在窗口中。

以下是代码:

<Canvas>
        <Grid Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <StackPanel Orientation="Horizontal" Grid.Row="0" FlowDirection="RightToLeft" HorizontalAlignment="Right" Background="#FF2B2B2B" Margin="0,0,20,0">
                <Button FontFamily="Marlett" FontSize="13" VerticalAlignment="Top" Foreground="White" Command="{x:Static SystemCommands.CloseWindowCommand}" Background="#FF2B2B2B">r</Button>
                <Button FontFamily="Marlett" FontSize="13" VerticalAlignment="Top" Foreground="White" Command="{x:Static SystemCommands.MaximizeWindowCommand}" Background="#FF2B2B2B">1</Button>
                <Button FontFamily="Marlett" FontSize="13" VerticalAlignment="Top" Foreground="White" Command="{x:Static SystemCommands.MinimizeWindowCommand}" Background="#FF2B2B2B">0</Button>
            </StackPanel>
        </Grid>
</Canvas>

1 个答案:

答案 0 :(得分:1)

像HighCore所说,删除Canvas并为第一行添加额外的Grid。 这应该是有效的。在RowDefinitions和ColumnDefinitions中查看高度和宽度。

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

    <Grid Grid.Row="0" Background="CadetBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="title" TextAlignment="Center"/>

        <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"  Background="#FF2B2B2B">
            <Button FontFamily="Marlett" FontSize="13" VerticalAlignment="Top" Foreground="White" Command="{x:Static SystemCommands.CloseWindowCommand}" Background="#FF2B2B2B">r</Button>
            <Button FontFamily="Marlett" FontSize="13" VerticalAlignment="Top" Foreground="White" Command="{x:Static SystemCommands.MaximizeWindowCommand}" Background="#FF2B2B2B">1</Button>
            <Button FontFamily="Marlett" FontSize="13" VerticalAlignment="Top" Foreground="White" Command="{x:Static SystemCommands.MinimizeWindowCommand}" Background="#FF2B2B2B">0</Button>
        </StackPanel>
    </Grid>

</Grid>