<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个控件(包括按钮)。
答案 0 :(得分:3)
Grid grid = (((Button)sender).Parent) as Grid;
if(grid != null)
{
ListBox listbox = grid.Parent as ListBox;
if(listbox != null)
{
listbox.Children.Remove(grid);
}
}
答案 1 :(得分:1)
myListbox.Items.Remove(((发件人为FrameworkElement).Parent)
答案 2 :(得分:0)
试试这个
Grid _grid = (Grid)(((Button)sender).Parent);
ListBox box = _grid.Parent as ListBox;
box.Children.Remove(_grid);