简体   繁体   English

通过单击用户控件上的按钮来删除它

[英]Removing a usercontrol by clicking a button on it

I have a GridView which shows certain items in it. 我有一个GridView,其中显示了某些项目。 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. 蓝色矩形代表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. 在添加断点然后检查时,我发现this.Parent为null。 I am stuck.How do I proceed? 我被卡住了,我该如何进行? I am using c#, xaml, winrt, visual studio 2012 我正在使用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. 但是,应该通过操作绑定到ViewModel中GridView的ItemsSource的ObservableCollection来完成从GridView中删除项目的操作。 Not like this, probably... 可能不是这样

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

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