Re-enable LibraryBar Item after touch on a ScatterViewItem that derives from that same LibraryBar

StackOverflow https://stackoverflow.com/questions/14283095

  •  15-01-2022
  •  | 
  •  

Frage

I have a MainWindow.xaml with a ScatterView which has a TagVisualization.xaml that appears when a tag is placed. Inside that TagVisualization, I have a PhotoGallery.xaml that has a LibraryBar, which is populated by an outside class called PhotoGalleryViewModel.cs. I have implemented the DragDropScatterView class so I can drag the items from the LibraryBar and drop them on the ScatterView. When a new ScatterViewItem is created it has a close button. When I click it the item is supposed to be removed from the ScatterView and re-enabled on the LibraryBar. My problem is with the re-enabling of the item, because I can't seem to get to the PhotoGallery.xaml.

A while ago I had something similar and someone gave me the following solution:

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);
    }

However, in my present project, this does not work because the _host.Tag is always null. I managed to come up with this:

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);

    }

}

But the problem with this (apart from it's inneficiency, because I come all the way up to the TagVisualization and go all the way to get the LibraryBar) is that I can't differentiate different LibraryBar's, i.e., if I have two tags on the surface, only one of them will get the item re-enabled, the others just don't do aything.

So my question is: Given the first chunck of code, how can I make it work for me? How can I, from there, reach my LibraryBar, i.e., go all the way from the ScatterViewItem (PhotoGalleryViewModel) to the LibraryBar that's inside a TagVisualization, that's, in it's turn, is inside a MainWindow with a ScatterView?

War es hilfreich?

Lösung

I managed to solve my problem, on my DragDropScatterView.cs I need to save my dragSource to the ScatterViewItem, so I can later on activate it. So I add the following code:

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

And this way I can use the first part of code displayed on my first post, because now I have the dragSource.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top