我有一个MainWindow.xamlwith a ScatterView,其中有一个TagVisualization.xaml在放置标签时出现的 a 。在里面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);
}
}
但是这个问题(除了它的效率低下,因为我一路走到 theTagVisualization并且一路走来得到LibraryBar)是我无法区分不同的LibraryBar's,即,如果我在表面上有两个标签,只有其中一个将重新启用该项目,而其他人则什么都不做。
所以我的问题是:给定第一块代码,我怎样才能让它为我工作?我怎样才能从那里到达我的,即从( )LibraryBar一路走到a 内部的那个,也就是说,轮到 a和 a 的内部?ScatterViewItemPhotoGalleryViewModelLibraryBarTagVisualizationMainWindowScatterView