2013-01-11 28 views
0

我有一個MainWindow.xamlScatterView其中有一個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,即從ScatterViewItemPhotoGalleryViewModel)到LibraryBar,這是TagVisualization的內部,也就是說,它在MainWindow的內部,有ScatterView

回答

0

我設法解決我的問題,在我DragDropScatterView.cs我需要將我的dragSource保存到ScatterViewItem,所以我可以稍後激活它。所以,我添加以下代碼:

private void OnCursorDrop(object sender, SurfaceDragDropEventArgs args) { 
    (...) 
    svi.Tag = droppingCursor.DragSource; 
    (...) 
} 

而這樣一來,我可以用在我的第一篇文章顯示代碼的第一部分,因爲現在我有dragSource

相關問題