db2
db2

Reputation: 517

WPF GridSplitter sizing with MinWidth

I'm trying to make a very simple two-column window, in which the right column starts at width 150, with a minimum width of 150, the left column fills the remaining space, with a minimum width of 300, and a GridSplitter allows resizing the two columns.

I'm running into two issues here.

  1. When attempting to drag the GridSplitter to the left more than the left column's MinWidth allows, the GridSplitter stops as expected, but the right column keeps growing and overflowing the right bounds of the window and getting clipped.

  2. When specifying a MinWidth for the window itself (which is equal to the MinWidth of the two content columns plus the column for the GridSplitter), I can resize the window just slightly smaller than I should be able to (maybe by 5-10 pixels), causing some of the right column's content to be clipped.

Are there some simple changes I need to make for this to behave properly?

<Window x:Class="CustomLabels.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:CustomLabels"
        mc:Ignorable="d"
        Title="Custom Labels" Height="350" Width="550" MinWidth="455" MinHeight="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="300" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="150" MinWidth="150" />
        </Grid.ColumnDefinitions>
        <TextBox x:Name="textBox" Height="23" Margin="81,14,90,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Grid.Column="0"/>
        <Label x:Name="label" Content="Order No." HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold" Grid.Column="0"/>
        <Button x:Name="button" Content="Load" Margin="0,14,10,0" VerticalAlignment="Top" Height="23" HorizontalAlignment="Right" Width="75" Grid.Column="0"/>

        <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Center" VerticalAlignment="Stretch" />

        <Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold" Width="47" Grid.Column="2" />
        <ListBox x:Name="listBox" Margin="10,41,10,10" IsSynchronizedWithCurrentItem="True" Grid.Column="2" />
    </Grid>
</Window>

Note that I have attempted to implement the solution shown in this question, but it doesn't appear to have any effect when I want the right column to start with a fixed width.

Upvotes: 2

Views: 1896

Answers (1)

db2
db2

Reputation: 517

Stupid XAML trick to the rescue. This looks slightly idiotic, but gives something close enough to the desired result:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" MinWidth="300" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="0*" MinWidth="150" />
    </Grid.ColumnDefinitions>
    ...
</Grid>

I guess that's telling it "Use as little space as possible, but don't go below 150, which ends up being the column's starting size.

I'm still getting the clipping when shrinking the window; that's probably some kind of misunderstanding of padding and/or margin.

Upvotes: 2

Related Questions