2013-04-25 201 views
1

我有一個GridView顯示其中的某些項目。每個項目都有一個編輯按鈕,按下編輯按鈕在該項目旁邊加載一個用戶控件。這個用戶控件有一個關閉按鈕,按下關閉按鈕可以刪除用戶控件。 GridView with the items(blue color rectangles)通過點擊一個按鈕刪除一個用戶控件

GridView after the edit button on the item(blue color rectangles) is clicked and usercontrol(red color rectangle) is loaded next to it

藍色長方形代表在GridView的項目。在點擊粉紅色的添加按鈕時,紅色的用戶控件加載。現在單擊紅色矩形上的關閉按鈕,它應該移除紅色的顏色項目並恢復到原始狀態。 我已經添加了usercontrol使用模板選擇器,它工作正常。但關閉按鈕不會將其刪除。我曾嘗試以下方法去除:

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

上添加一個斷點,然後檢查我發現this.Parent爲空。我卡住了,我該怎麼辦?我使用C#,XAML,WinRT中時,Visual Studio 2012

+0

我聽說父項屬性沒有在構造函數中自動設置。您是否嘗試過在設置控件後手動設置父級?像'yourobj.parent = topobject'? – Julian 2013-04-25 10:10:31

+0

嗨@Julian Thanx :)其實這是不可能的,因爲usercontrol基本上是一個數據模板。我改變了現在解決了我的問題的datacontext :) – Vanya 2013-04-25 10:59:43

+0

標記正確答案被認爲是禮貌的。 – 2013-08-12 19:49:24

回答

0

如果你需要工作了可視化樹,試試這個:

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

但是從一個GridView刪除項目應通過操縱一個ObservableCollection綁定完成到ViewModel中的GridView的ItemsSource。不是這樣,大概...

相關問題