user3735822
user3735822

Reputation: 335

How to remove previously updated UIElement on grid row to add new UIElement?

I am working in silverlight where i have a grid called "bigGrid" which contains three rows and on the first row i have combobox and i update two UI elemnt on two different rows on Loading the comboBox (i mean second row bigGrid(because bigGrid is parent of all) or first and second row of rowGrid in my code).

Now on selectionChanged Event of combobox i have to replace the previously rendered UI elemnts with the UI element selected from combo box (If the UIelement is one it will be displayed on one row and if the UI elements are two they will be displayed on two different rows one after other (Please note that on Loading this combobox i display 2 UI elemnts in 2 consecutive rows.)

Now Problem ?: The problem in when i load combobox the grids are intialised with two UIelement on 2 rows. but on Selectionchnaged event when i render 2 uielements then it wroks fine for 2 UIElements (it replaces the previous rendering of Loaded event on both rows . But the problem is when i dispaly only 1 UIElement in first row because MY RECENTLY RENDERED uiElement on selection changed event is no doubt updated in first row but the UI element of second row (from combobox Loaded even still persists).

How to delete this PREVIOUSLY persisting UI element ?

My code for (please note that i have given just useful code. Ofcourse combe is declared somewhere and it has Items as well)

public Grid somefunction() //this function returs the final bigGrid (which contains all the ROWS CONATINING ui ELEMNTS)
    {
      cmb.Loaded += (o3, e) =>
                {
                    foreach (object o in pv.Root.Parameter)
                    {
                        param = (Parameter)o;
                        if (o is Parameter)
                        {
                         rowGrid = IntializeUIElements(param, atrbt);
                        } 
                        Grid.SetRow(rowGrid, loopCount);
                    }
                        bigGrid.Children.Add(rowGrid);
                        loopCount++;
                };




     cmb.SelectionChanged += (o1, e) =>
                {
                    string selectedComboItem = cmb.SelectedItem.ToString();
                    Grid storeRowGrid = new Grid();
                    for (int i = 0; i < pv.Root.Parameter.Count; i++)
                    {
                        storeRowGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                    }
                    int count = 0;
                    foreach (object o in pv.Root.Parameter)
                    {
                        if (o is Parameter)
                        {
                            rowGrid = IntializeUIElements(param, atrbt); //It returns the grid with UI element
                            Grid.SetRow(rowGrid, count);
                            storeRowGrid.Children.Add(rowGrid);
                            Grid.SetRow(storeRowGrid, count);
                            if (bigGrid.Children.Count > 1)
                            {
                                bigGrid.Children.RemoveAt(bigGrid.Children.Count - 1); //this is to remocve previous item on selection change
                            }
                            count++;
                        }

                    }             

                             bigGrid.Children.Add(storeRowGrid);
                };

                Grid.SetColumn(cmb, 1);
                comboRowGrid.Children.Add(cmb);
                Grid.SetRow(comboRowGrid, 0);
                bigGrid.Children.Add(comboRowGrid); //This BigGrid is parent Grid.
                return bigGrid;
    }

How to clear the previous UI element of Loaded event on second row when selection changed event show just & uielement in first row ?

Upvotes: 1

Views: 196

Answers (1)

Mashton
Mashton

Reputation: 6415

Your layout is something like this:

<Grid>
  <ComboBox Grid.Row="0"/>
  <SomeControl Grid.Row="1"/>
  <SomeOtherControl Grid.Row="2"/>
   ...
</Grid>

If you made it more like this:

<Grid>
  <ComboBox Grid.Row="0"/>
  <Grid x:Name="grid with controls chosen by the Combobox selection" Grid.Row="1">
    <SomeControl Grid.Row="0"/>
    <SomeOtherControl Grid.Row="1"/>
  </Grid>
   ...
</Grid>

You wouldn't have to worry about how many controls to remove, because you can just remove/replace a single grid.

Upvotes: 1

Related Questions