【问题标题】:Delete the parent of a child删除孩子的父母
【发布时间】:2012-11-27 11:50:22
【问题描述】:
<ListBox>
    <Grid>
        <Button Name="button1" Click="buttonDelete_Click"></Button>
    </Grid>
    <Grid>
        <Button Name="button2" Click="buttonDelete_Click"></Button>
    </Grid>
    <Grid>
        <Button Name="button3" Click="buttonDelete_Click"></Button>
    </Grid>
    <Grid>
        <Button Name="button4" Click="buttonDelete_Click"></Button>
    </Grid>
</ListBox>

我正在使用按钮单击事件,我想从列表框中删除Grid ((Button)sender).Parent,这样做我还删除了网格的所有子项,包括按钮本身。

请注意,在我的应用程序中,我使用 c# 在代码中的网格上添加了 19 个控件(包括按钮)。

【问题讨论】:

  • 很酷的东西,但问题出在哪里???
  • 您是否从父属性中获取 Grid 对象?

标签: c# xaml windows-phone


【解决方案1】:
Grid grid =   (((Button)sender).Parent) as Grid;
if(grid != null)
{
   ListBox listbox = grid.Parent as ListBox;
   if(listbox != null)
   {
     listbox.Children.Remove(grid);
   }
}

【讨论】:

    【解决方案2】:

    myListbox.Items.Remove(((sender as FrameworkElement).Parent)

    【讨论】:

      【解决方案3】:

      试试这个

      Grid _grid =   (Grid)(((Button)sender).Parent);
      ListBox box = _grid.Parent as ListBox;
      box.Children.Remove(_grid);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 2019-02-02
        • 2012-09-12
        • 2015-01-04
        • 1970-01-01
        • 2014-03-02
        • 1970-01-01
        相关资源
        最近更新 更多