wpf - GridSplitter MinWidth with fixed size -
i have grid 2 columns separated gridsplitter using 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 : minwidth of column on right ignored
- i need first column width "100px" when page loads, cannot * sized.
- i not want set maxwidth on first column
*i know has been adressed before suggest set column size * or set maxwidth on first column... don't want that.
found solution, but ugly! :p, has cleaner way achieve 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 grid splitter works adjusting width of left column only , assumes right column star-size fit remaining space.
that means problem trying solve "how limit max width of left column right column not small?". code sample doing.
if want more portable solution, can implement in xaml, create silverlight behavior can applied grid (as shown below). attach parent grid's sizechanged
event , pretty code snippet does, being behavior can drag , drop them in blend or attach them in xaml.
here sample behavior threw example (based on own code):
minwidthsplitterbehavior.cs
using system; using system.windows; using system.windows.controls; using system.windows.interactivity; namespace gridsplitterminwidth { public class minwidthsplitterbehavior : behavior<grid> { public grid parentgrid { get; set; } protected override void onattached() { base.onattached(); parentgrid = this.associatedobject grid; parentgrid.sizechanged += parent_sizechanged; } void parent_sizechanged(object sender, sizechangedeventargs e) { if (parentgrid.columndefinitions.count == 3) { double maxw = e.newsize.width - parentgrid.columndefinitions[2].minwidth - parentgrid.columndefinitions[1].actualwidth; parentgrid.columndefinitions[0].maxwidth = maxw; } } protected override void ondetaching() { base.ondetaching(); if (parentgrid != null) { parentgrid.sizechanged -= parent_sizechanged; } } } }
and use this:
<usercontrol x:class="gridsplitterminwidthapp.mainpage" 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:gridsplitterminwidth="clr-namespace:gridsplitterminwidth" xmlns:interactivity="clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity" xmlns:controls="clr-namespace:system.windows.controls;assembly=system.windows.controls" mc:ignorable="d" d:designheight="300" d:designwidth="400"> <grid x:name="layoutroot" background="white"> <grid> <grid.columndefinitions> <columndefinition width="100" minwidth="20" /> <columndefinition width="10" /> <columndefinition width="*" minwidth="100" /> </grid.columndefinitions> <interactivity:interaction.behaviors> <gridsplitterminwidth:minwidthsplitterbehavior/> </interactivity:interaction.behaviors> <rectangle fill="blue" /> <controls:gridsplitter grid.column="1" background="lightgray" horizontalalignment="stretch"> </controls:gridsplitter> <rectangle fill="yellow" grid.column="2" /> </grid> </grid> </usercontrol>
Comments
Post a Comment