我有一個MainWindow.xaml
與ScatterView
其中有一個TagVisualization.xaml
當出現標籤放置時出現。在TagVisualization
的內部,我有一個PhotoGallery.xaml
,它有一個LibraryBar
,由一個名爲PhotoGalleryViewModel.cs
的外部類填充。我已經實施了DragDropScatterView
職業,所以我可以將這些物品從LibraryBar
拖放到ScatterView
上。當一個新的ScatterViewItem
被創建時,它有一個關閉按鈕。當我點擊它時,該項目應該從ScatterView
中刪除,並在LibraryBar
上重新啓用。我的問題是重新啓用該項目,因爲我似乎無法進入PhotoGallery.xaml
。重新啓用一個ScatterViewItem後觸發一個ScatterViewItem,從同一個LibraryBar
前一段時間我有類似的東西,有人給了我以下解決方案:
private void SurfaceButton_TouchDown(object sender, TouchEventArgs e) {
ScatterViewItem _host = MyApplication.Helpers.VisualTree.FindVisualParent<ScatterViewItem>(this);
if (_host != null) {
DependencyObject parent = VisualTreeHelper.GetParent(this);
ScatterViewItem svi = null;
while (parent as DragDropScatterView == null)
{
if (parent is ScatterViewItem)
svi = parent as ScatterViewItem;
parent = VisualTreeHelper.GetParent(parent);
}
// Access directly to the LibraryBar
LibraryBar lb = _host.Tag as LibraryBar;
lb.SetIsItemDataEnabled(_host.Content, true);
((DragDropScatterView)parent).Items.Remove(this.DataContext);
}
然而,在我目前的項目,這不起作用,因爲_host.Tag
總是null
。 我設法想出這個:
private void scatterCloseButton(object sender, TouchEventArgs e) {
ScatterViewItem _host = MyApplication.Model.VisualTree.FindVisualParent<ScatterViewItem>(this);
if (_host != null) {
DependencyObject parent = VisualTreeHelper.GetParent(this);
ScatterViewItem svi = null;
while (parent as DragDropScatterView == null) {
if (parent is ScatterViewItem)
svi = parent as ScatterViewItem;
parent = VisualTreeHelper.GetParent(parent);
}
// The data of the item
PhotoGalleryViewModel lb = _host.DataContext as PhotoGalleryViewModel;
if (lb != null) {
// The Tag Visualizer relative to that tag
TagVisualizer tagVisualizer = SurfaceFiturApp.Model.VisualTree.FindVisualParent<TagVisualizer>(this);
if (tagVisualizer != null) {
// The PhotoGallery object where the gallery is in
PhotoGallery photoGallery = SurfaceFiturApp.Model.VisualTree.FindVisualChild<PhotoGallery>(tagVisualizer);
if (photoGallery != null) {
// Enable the item in the library
photoGallery.setLibraryItemEnabled(lb);
}
}
}
// Remove the object from the ScatterView
((DragDropScatterView)parent).Items.Remove(this.DataContext);
}
}
但有了這個(除了它的inneficiency,因爲我大老遠到TagVisualization
並一路得到LibraryBar
)問題是我無法區分不同的LibraryBar's
,也就是說,如果表面上有兩個標籤,則只有其中一個可以重新啓用該項目,而其他人則不會做任何事情。
所以我的問題是:鑑於第一塊代碼,我該如何讓它爲我工作?我怎麼能從那裏到達我的LibraryBar
,即從ScatterViewItem
(PhotoGalleryViewModel
)到LibraryBar
,這是TagVisualization
的內部,也就是說,它在MainWindow
的內部,有ScatterView
?