This morning I’m was working on a grid that has a GridSplitter to split the panes, but needed a quick way to collapse the left side to get it out of the way – and when finished, restore it to its previous size. Something like the following scenario:
The solution for that is trivially simple:
DateTime lastClicked;
GridLength cachedColumnWidth;
void GridSplitter_MouseLeftButtonUp(
object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//The Poor Man's DoubleClick:
if (DateTime.Now.Subtract(lastClicked).TotalMilliseconds < 500)
{
GridSplitter splitter = sender as GridSplitter;
//What column is it in?
int colIndex = (int)splitter.GetValue(Grid.ColumnProperty);
//And what grid are we talking about?
Grid grid = splitter.Parent as Grid;
Debug.Assert(grid.ColumnDefinitions.Count > 0);
//Cool...now look at the column definition:
GridLength gridLength = grid.ColumnDefinitions[colIndex].Width;
//In order to see if we've already slammed the splitter up against the side:
GridLength splitterWidth = new GridLength(splitter.Width);
if (gridLength.Equals(splitterWidth))
{
//Already collapsed..restore:
grid.ColumnDefinitions[colIndex].Width = cachedColumnWidth;
}else{
//Collapse the column:
cachedColumnWidth = gridLength;
grid.ColumnDefinitions[colIndex].Width = splitterWidth;
}
}
lastClicked = DateTime.Now;
}
Links:
http://downloads.xact-solutions.com/Silverlight/Posts/100125b/XAct.Studies.GridSplitterCollapseEx.xap