private static void UpdateGrid(Grid grid, GridSplitter gridSplitter, bool isVisible) { // Get the definitions of interest. These include the splitter's and its neighbors. var index = gridSplitter.GetIndex(); var definitions = grid.GetDefinitions(gridSplitter); var splitterDefinition = definitions.ElementAt(index); var previousDefinition = definitions.ElementAt(gridSplitter.GetPreviousIndex(index)); var nextDefinition = definitions.ElementAt(gridSplitter.GetNextIndex(index)); var gridLengthsCache = GetGridSplitterLengthsCache(grid); if (gridLengthsCache == null) { // No cache, so create and store one. gridLengthsCache = new WeakKeyDictionary <DefinitionBase, GridLength>(); SetGridSplitterLengthsCache(grid, gridLengthsCache); } if (gridLengthsCache.ContainsKey(splitterDefinition) && gridLengthsCache.ContainsKey(previousDefinition) && gridLengthsCache.ContainsKey(nextDefinition)) { if (isVisible) { // Restore to cached size. splitterDefinition.SetLength(gridLengthsCache[splitterDefinition]); previousDefinition.SetLength(gridLengthsCache[previousDefinition]); nextDefinition.SetLength(gridLengthsCache[nextDefinition]); } else { // Ensure collapsed. Update the cached sizes if necessary. splitterDefinition.HideDefinitionAndCacheLength(gridLengthsCache); previousDefinition.HideDefinitionAndCacheLength(gridLengthsCache); nextDefinition.HideDefinitionAndCacheLength(gridLengthsCache); } } else { // We haven't cached the GridLength values, or at least one of the cached definitions somehow went bad. Store the current values. // Since the cache is out-of-date, leave everything alone. gridLengthsCache[splitterDefinition] = splitterDefinition.GetLength(); gridLengthsCache[previousDefinition] = previousDefinition.GetLength(); gridLengthsCache[nextDefinition] = nextDefinition.GetLength(); } }