I have a Grid with 2 columns separated by a GridSplitter using the following XAML code :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" MinWidth="20" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" MinWidth="100" />
</Grid.ColumnDefinitions>
<Rectangle Fill="Blue" />
<GridSplitter Grid.Column="1" Background="LightGray" HorizontalAlignment="Stretch" />
<Rectangle Fill="Yellow" Grid.Column="2" />
</Grid>
Problem : The MinWidth of the Column on the right is ignored
- I definitely need the first column Width to be “100px” when page loads, so It cannot be * sized.
- I do not want to set a MaxWidth on the first column
*I know that has been adressed before but it always suggest to set column size to * or set a maxWidth on the first column… I don’t want that.
Found a solution, but its UGLY! :p, anybody has a cleaner way to achieve what I want… CODELESS (if possible)?
private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
var g = (Grid)sender;
Double maxW = e.NewSize.Width - g.ColumnDefinitions[2].MinWidth - g.ColumnDefinitions[1].ActualWidth;
g.ColumnDefinitions[0].MaxWidth = maxW;
}
The basic problem is that a grid splitter works by adjusting the width of the left column only and assumes the right column will star-size to fit the remaining space.
That means the problem you are trying to solve is actually “how do I limit the max width of the left column so that the right column does not get too small?”. This is basically what your code sample is doing.
If you want a more portable solution, that you can implement in XAML, create a Silverlight behavior that can be applied to a grid (as shown below). It will attach to the parent grid’s
SizeChangedevent and do pretty much exactly what your code snippet does, but being a behavior you can drag and drop them in Blend or attach them in XAML.Here is a sample behavior I threw together for you as an example (based on your own code):
MinWidthSplitterBehavior.cs
and use it like this: