I am having an issue when I change my ColumnDefinitions at runtime. When I do, the columns Width does not reflect immediately. If I resize the current window/usercontrol then the Width does reflect.
I am following MVVM as well, I have a DependencyProperty that allows me to change them as needed on the Grid.
Currently here is what I am using and does work if I resize the window and or control the Grid is on...
public static void ColumnSpacerChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (!(obj is Grid) || ((Grid)obj).ColumnDefinitions.Count == 0 || (int)e.NewValue == (int)e.OldValue)
return;
Grid grid = (Grid)obj;
for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
{
if(grid.ColumnDefinitions[i].Tag != null && grid.ColumnDefinitions[i].Tag.ToString() == COLUMN_SPACER)
{
grid.ColumnDefinitions[i] = new ColumnDefinition() { Width = new GridLength((int)e.NewValue, GridUnitType.Pixel), Tag = COLUMN_SPACER };
}
}
grid.UpdateLayout(); // Tried this, but doesn't work
}
I have tried the following and none work.
grid.UpdateLayout();
grid.Refresh; // (off the parent)
Is there something I am missing when I change the ColumnDefinitions during run-time they immediately do not reflect?
I found the solution with some help from @Mike Strobel....
This needs to be called to invalidate the arrange state (layout) for the element. After this invalidation the element will then have its layout updated and reflected asynchronously.