简体   繁体   English

如何删除网格行上以前更新的UIElement以添加新的UIElement?

[英]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). 我在Silverlight中工作,其中有一个名为“ bigGrid”的网格,其中包含三行,第一行有combobox,并且在加载comboBox时,我在两个不同的行上更新了两个UI elemnt(我的意思是第二行bigGrid(因为bigGrid是的父级)或我的代码中rowGrid的第一和第二行)。

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.) 现在在组合框的selectionChanged Event上,我必须用从组合框中选择的UI元素替换以前呈现的UI元素(如果UIelement是1,它将显示在一行上;如果UIelement是2,则它们将显示在两个不同的行上)一行接一行(请注意,在加载此组合框时,我在连续的2行中显示2个UI元素。)

Now Problem ?: The problem in when i load combobox the grids are intialised with two UIelement on 2 rows. 现在问题?:问题是当我加载组合框时,在2行中使用两个UIelement初始化网格。 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). 但是在Selectionchnaged事件上,当我渲染2个uielements时,它对2个UIElements正常工作(它替换了先前在两行上渲染的Loaded事件。 但是问题是当我在第一行中仅显示1个UIElement时,因为我最近选择的uiElement渲染了uiElement事件无疑会在第一行中更新,但第二行的UI元素(从组合框“加载”中仍然存在)。

How to delete this PREVIOUSLY persisting UI element ? 如何删除此先前持久的UI元素?

My code for (please note that i have given just useful code. Ofcourse combe is declared somewhere and it has Items as well) 我的代码(请注意,我只给出了有用的代码。当然,combe在某处声明,并且也有Items)

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 ? 当选择更改的事件在第一行显示just&uielement时,如何清除第二行的Loaded事件的上一个UI元素?

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. 您不必担心要删除多少控件,因为您只需删除/替换单个网格即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM