Search code examples
c#wpfgridstackpanel

WPF grid size changed event firing only when increasing and not when decreasing


I have a grid with rowdefinitions

RowDefinition rd0 = new RowDefinition();
rd0.Height = new GridLength(1, GridUnitType.Auto);
RowDefinition rd1 = new RowDefinition();
rd1.Height = new GridLength(1, GridUnitType.Star);
RowDefinition rd2 = new RowDefinition();
rd2.Height = new GridLength(1, GridUnitType.Auto);
grdMain.RowDefinitions.Add(rd0);
grdMain.GetGrdPlugins().RowDefinitions.Add(rd1);
grdMain.GetGrdPlugins().RowDefinitions.Add(rd2);

now in the first row I add a textblock

var tbxTitle = new TextBlock(){};
Grid.SetRow(tbxTitle, 0);
grdMain.Children.Add(tbxTitle);

in the third a stack panel of buttons

StackPanel spButtons = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center, };
grdMain.Children.Add(spButtons);
Grid.SetRow(spButtons, 2);
...

in the second a stackpanel.

Now I want an event to be called all the times the grid changes in size:

var spMatrix_Volatile = new StackPanel() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, Background = Brushes.RosyBrown};
spMatrix_Volatile.SizeChanged += (sender, args) =>
{
    Console.Beep();
    double dHeight = spMatrix_Volatile.ActualHeight;
    CreateCellMatrix(out strResult, ref spMatrix_Volatile, false, dHeight);
};

Grid.SetRow(spMatrix_Volatile, 1);
grdMain.Children.Add(spMatrix_Volatile);

now the peculiar thing is that the size changed event is called all the times the height of the grid is increased but never when decreased.

Thank you


Solution

  • That happens to me as well using your code. I have therefore put the stackpanel in a grid and that didn't happen anymore. That is the event is fired both when increasing and decreasing.