private void ResizeRows(double delta)
{
//if no change just return
if (delta.CompareTo(0D) == 0) return;
//Only works when the grid has at least 3 rows
int containingGridRows = _containingGrid.RowDefinitions.Count;
if (containingGridRows < 3) return;
int row = Grid.GetRow(this);
if (row < 1) return; //Do nothing if in top row - introduce ResizeBehavior
if (row == containingGridRows - 1) return; //Do nothing if in last row - introduce ResizeBehavior
int upperRow = row - 1;
int lowerRow = row + 1;
bool isUpperContentMaxed;
bool isLowerContentMaxed;
ScrollViewer upperScrollViewer = GetScrollViewer(_containingGrid, upperRow);
ScrollViewer lowerScrollViewer = GetScrollViewer(_containingGrid, lowerRow);
if (upperScrollViewer != null)
isUpperContentMaxed = upperScrollViewer.ExtentHeight.CompareTo(upperScrollViewer.ViewportHeight) == 0;
else
{
ScrollBar upperScrollBar = GetScrollBar(_containingGrid, upperRow);
isUpperContentMaxed = upperScrollBar.Visibility == Visibility.Collapsed;
}
if (lowerScrollViewer != null)
isLowerContentMaxed = lowerScrollViewer.ExtentHeight.CompareTo(lowerScrollViewer.ViewportHeight) == 0;
else
{
ScrollBar lowerScrollBar = GetScrollBar(_containingGrid, lowerRow);
isLowerContentMaxed = lowerScrollBar.Visibility == Visibility.Collapsed;
}
if (delta < 0 && isLowerContentMaxed) return;
if (delta > 0 && isUpperContentMaxed) return;
RowDefinition upperRowDefinition = _containingGrid.RowDefinitions[upperRow];
RowDefinition lowerRowDefinition = _containingGrid.RowDefinitions[lowerRow];
double upperRowActualHeight = upperRowDefinition.ActualHeight;
double lowerRowActualHeight = lowerRowDefinition.ActualHeight;
double upperRowMaxHeight = upperRowDefinition.MaxHeight;
double lowerRowMaxHeight = lowerRowDefinition.MaxHeight;
double upperRowMinHeight = upperRowDefinition.MinHeight;
double lowerRowMinHeight = lowerRowDefinition.MinHeight;
delta = AdjustDelta(upperRowActualHeight, upperRowMinHeight, upperRowMaxHeight, 1, delta);
delta = AdjustDelta(lowerRowActualHeight, lowerRowMinHeight, lowerRowMaxHeight, -1, delta);
double newUpperRowActualHeight = upperRowActualHeight + delta;
double newLowerRowActualHeight = lowerRowActualHeight - delta;
double requestedActualHeight = newUpperRowActualHeight + newLowerRowActualHeight;
_containingGrid.BeginInit();
double upperRatio = newUpperRowActualHeight/requestedActualHeight;
double lowerRatio = newLowerRowActualHeight/requestedActualHeight;
upperRowDefinition.Height = new GridLength(upperRatio, GridUnitType.Star);
lowerRowDefinition.Height = new GridLength(lowerRatio, GridUnitType.Star);
_containingGrid.EndInit();
}