Search code examples
wpfgridgridsplitter

Is there a way to have the GridSplitter not to push elements out of the window?


With the XAML below when I drag the GridSplitter to the left it pushes elements out of the window. How can I keep all elements within the window frame?

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>

    <Button Grid.Column="0" Content="0" />
    <Button Grid.Column="1" Content="1" />
    <Button Grid.Column="2" Content="2" />
    <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Left" />
</Grid>

Thanks


Solution

  • The only way I know to solve your problem is have the columns that are left and right of your gridsplitter have the width property set as Width="*" and give the GridSplitter its own column with a HorizontalAlignment set as HorizontalAlignment="Stretch". Your code would then end up looking like this.

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
    
        <Button Grid.Column="0" Content="0" />
        <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch"/>
        <Button Grid.Column="2" Content="1" />
        <Button Grid.Column="3" Content="2" />
    </Grid>