在触及来自同一个LibraryBar的ScatterViewItem后重新启用LibraryBar Item
本文关键字:LibraryBar 新启用 Item 启用 ScatterViewItem 同一个 | 更新日期: 2023-09-27 17:51:18
我有一个MainWindow.xaml
, ScatterView
有一个TagVisualization.xaml
,当一个标签被放置时出现。在TagVisualization
内部,我有一个PhotoGallery.xaml
,它有一个LibraryBar
,它由一个叫做PhotoGalleryViewModel.cs
的外部类填充。我已经实现了DragDropScatterView
类,所以我可以从LibraryBar
中拖动项目并将它们放在ScatterView
上。当一个新的ScatterViewItem
被创建时,它有一个关闭按钮。当我单击它时,该项目应该从ScatterView
中删除并在LibraryBar
上重新启用。我的问题是重新启用项目,因为我似乎无法到达PhotoGallery.xaml
.
前段时间我有类似的事情,有人给了我下面的解决方案:
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);
}
}
但是这样做的问题(除了效率低下,因为我一直到TagVisualization
,然后一直到LibraryBar
)是我无法区分不同的LibraryBar's
,也就是说,如果我在表面上有两个标签,只有其中一个会让项目重新启用,其他的什么都不做。
所以我的问题是:给定第一段代码,我如何使它为我工作?我怎么能从那里到达我的LibraryBar
,也就是说,从ScatterViewItem
(PhotoGalleryViewModel
)一直到LibraryBar
,它在TagVisualization
里面,也就是说,反过来,在MainWindow
里面有ScatterView
?
我设法解决了我的问题,在我的DragDropScatterView.cs
上,我需要将我的dragSource
保存到ScatterViewItem
,这样我就可以稍后激活它。所以我添加了以下代码:
private void OnCursorDrop(object sender, SurfaceDragDropEventArgs args) {
(...)
svi.Tag = droppingCursor.DragSource;
(...)
}
这样我就可以使用我第一篇文章中显示的第一部分代码,因为现在我有了dragSource
.