簡體   English   中英

如何刪除網格行上以前更新的UIElement以添加新的UIElement?

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

我在Silverlight中工作,其中有一個名為“ bigGrid”的網格,其中包含三行,第一行有combobox,並且在加載comboBox時,我在兩個不同的行上更新了兩個UI elemnt(我的意思是第二行bigGrid(因為bigGrid是的父級)或我的代碼中rowGrid的第一和第二行)。

現在在組合框的selectionChanged Event上,我必須用從組合框中選擇的UI元素替換以前呈現的UI元素(如果UIelement是1,它將顯示在一行上;如果UIelement是2,則它們將顯示在兩個不同的行上)一行接一行(請注意,在加載此組合框時,我在連續的2行中顯示2個UI元素。)

現在問題?:問題是當我加載組合框時,在2行中使用兩個UIelement初始化網格。 但是在Selectionchnaged事件上,當我渲染2個uielements時,它對2個UIElements正常工作(它替換了先前在兩行上渲染的Loaded事件。 但是問題是當我在第一行中僅顯示1個UIElement時,因為我最近選擇的uiElement渲染了uiElement事件無疑會在第一行中更新,但第二行的UI元素(從組合框“加載”中仍然存在)。

如何刪除此先前持久的UI元素?

我的代碼(請注意,我只給出了有用的代碼。當然,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;
    }

當選擇更改的事件在第一行顯示just&uielement時,如何清除第二行的Loaded事件的上一個UI元素?

您的布局是這樣的:

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

如果您更像這樣:

<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>

您不必擔心要刪除多少控件,因為您只需刪除/替換單個網格即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM