简体   繁体   中英

Removing a usercontrol by clicking a button on it

I have a GridView which shows certain items in it. Each item has a edit button into it, pressing the edit button loads a usercontrol next to the item. This usercontrol has a close button into it, pressing the close button removes the usercontrol. 带有项目的GridView(蓝色矩形)

单击项目(蓝色矩形)上的编辑按钮并在其旁边加载用户控件(红色矩形)后的GridView

Blue color rectangles represent the items in the gridview. On clicking the pink add button the red color usercontrol loads up. Now on the clicking the close button on the red rectangle it should remove the red color item and revert to the original state. I have added the usercontrol using template selector and it is working fine. But the close button does not remove it. I have tried the following approach for removing :

(GridView)this.Parent).Items.Remove(this);

On adding a breakpoint and then checking I found that this.Parent is null. I am stuck.How do I proceed? I am using c#, xaml, winrt, visual studio 2012

If you need to work up the visual tree, try this:

var _Parent = VisualTreeHelper.GetParent(sender as UserControl) as GridView;
_Parent.Items.Remove(sender as UserControl);

But removing items from a GridView should be done by manipulating an ObservableCollection bound to the ItemsSource of the GridView in the ViewModel. Not like this, probably...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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