0

I have a wpf application in which I am using a wpf grid and adding/removing controls dynamically to it. Adding controls is fine but upon removing they disappear from the grid but left the vacant place in them, which I want to be occupied by filled by rest of the elements, just like they did in wrap panel.

Previously I was using the wrap panel and it works fine, but I need to add splitter control among my groupboxes thus I replaced the wrappanel with grid.

Here are the screenshots of the window

enter image description here

I remove the middle groupbox and it displays like this

enter image description here

I want the third groupbox to occupy the place of second groupbox. For the record, I have used grid resize event and this is the code it is having

 foreach (var control in this.DynamicGrid.Children)
        {
            if (control.GetType() == typeof(GroupBox))
            {
                GroupBox groupBox = control as GroupBox;
                groupBox.Height = this.DynamicGrid.ActualHeight;
                this.WrapPanel1.Width = this.DynamicGrid.ActualWidth;
            }
        }

Wrappanel has dynamic grid in which we are adding/removing elements.

Manvinder
  • 4,495
  • 16
  • 53
  • 100

2 Answers2

0

You can set your <ColumnDefinition width="Auto" />

and use Visibility="Collapsed" to hide the column.

Clement Dungler
  • 737
  • 6
  • 10
0

I fail to see your problem... it seems as though you are asking 'How do I use a Grid control in code?'. If you want to alter the Grid.ColumnDefinitions (or any other Grid property) at runtime, then why not do just that? If you look at the Grid Class page on MSDN, you'll see a long code example where it shows you exactly how to manipulate a Grid in code. For example, you could do something like this:

ColumnDefinition rightHandColumn = new ColumnDefinition();
YourGrid.ColumnDefinitions.Add(rightHandColumn);
YourGrid.Children.Add(uiElementCurrentlyInGrid);
YourGrid.SetRow(uiElementCurrentlyInGrid, 0);
YourGrid.SetColumn(uiElementCurrentlyInGrid, 1);

Then to remove an item:

Grid.Children.Remove(uiElementCurrentlyInGrid);
Grid.ColumnDefinitions.Remove(rightHandColumn);
Sheridan
  • 68,826
  • 24
  • 143
  • 183