blgrnboy
blgrnboy

Reputation: 5157

WPF Grid Set To ActualWidth of Window, clipping occurs

I have found an answer here about how to make sure that when a Window is resized, the grid inside of it is resized accordingly as well. The problem I am facing is that I have a StackPanel inside of the grid, and the "X" (Close) button is not fully displaying (only about half of it is in the Window.

Here is the code:

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

Upvotes: 0

Views: 165

Answers (1)

Roland
Roland

Reputation: 398

Like HighCore said, remove the Canvas and put a extra Grid for the first Row. This should be work. Take a look for Height and Width in RowDefinitions and 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>

Upvotes: 1

Related Questions