在Surface上的ScatterView中使用LibraryStack
-
06-07-2019 - |
题
我们正在尝试弄清楚如何将项目从LibraryStack容器拖到ScatterView上,就像照片查看器示例应用程序的工作方式一样。目前,我们将它拖出后,该项目才会飞回LibraryStack。我们可以将项目拖放到其他LibraryStack或LibraryBars中。
以下是我们正在尝试的示例:
<s:SurfaceWindow x:Class="Idia_seminar.SurfaceWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
Title="Idia_seminar"
>
<s:SurfaceWindow.Resources>
<ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</s:SurfaceWindow.Resources>
<Grid Background="{StaticResource WindowBackground}" >
<s:ScatterView Name="scatterView1" AllowDrop="True">
<s:SurfaceButton Name="surfaceButton1">Button</s:SurfaceButton>
<s:LibraryStack AllowDrop="True">
<s:LibraryStackItem Content="hello"></s:LibraryStackItem>
</s:LibraryStack>
</s:ScatterView>
</Grid>
</s:SurfaceWindow>
谢谢!
解决方案
这当然可行。我编写了一个示例,允许您从散点图中的库栏中拖动项目,并将散点图上的项目放在它们显示为新的散点图项目的位置。
我不确定你哪里出错,但为了让拖放工作,必须发生以下情况:
- 放置目标必须将AllowDrop设置为true
- 点击目标必须可见才能进行测试(通常通过设置除null以外的背景来实现 - 我使用透明)
- 放置目标必须处理Drop事件并使用数据做一些聪明的事情 醇>
这是我的XAML:
<s:ScatterView AllowDrop="True" Background="Transparent"
x:Name="scatterView" s:SurfaceDragDrop.Drop="scatterView_Drop">
<s:SurfaceButton Name="surfaceButton1">Button</s:SurfaceButton>
<s:LibraryStack>
<s:LibraryStackItem Content="Hello"></s:LibraryStackItem>
</s:LibraryStack>
</s:ScatterView>
</s:ScatterView>
在代码中,我们处理Drop事件
private void scatterView_Drop(object sender, SurfaceDragDropEventArgs e)
{
Console.WriteLine("Got drop: " + e.Cursor.Data);
var newItem = new ScatterViewItem();
// Rely on .ToString() on the data. A real app would do something more clever
newItem.Content = e.Cursor.Data;
// Place the new item at the drop location
newItem.Center = e.Cursor.GetPosition(scatterView);
// Add it to the scatterview
scatterView.Items.Add(newItem);
}
显然,上面的代码不会处理将项目拖回库栏。我将其作为练习留给读者; - )
以下MSDN指南我绝对认为您应该阅读: http: //msdn.microsoft.com/en-us/library/ee804812.aspx
其他提示
您需要将散点图上的背景画笔设置为颜色,即透明 因为它甚至可以抓住掉落事件。
您还需要使用SurfaceDragDrop。
SurfaceDragDrop.AddDropHandler(scatterView1,OnCursorDrop); AddHandler(ScatterViewItem.ScatterManipulationStartedEvent,新的ScatterManipulationStartedEventHandler(OnManipulationStarted));
private void OnManipulationStarted(object sender, RoutedEventArgs args)
{ ScatterViewItem svi = args.OriginalSource as ScatterViewItem; if(svi!= null)//&amp;&amp; DragDropScatterView.GetAllowDrag(SVI)) { svi.BeginDragDrop(svi.DataContext); } }
private void OnCursorDrop(object sender, SurfaceDragDropEventArgs args)
{ SurfaceDragCursor droppingCursor = args.Cursor;
// Add dropping Item that was from another drag source.
if (!scatterView1.Items.Contains(droppingCursor.Data)){
scatterView1.Items.Add(droppingCursor.Data);
var svi = scatterView1.ItemContainerGenerator.ContainerFromItem(droppingCursor.Data) as ScatterViewItem;
if (svi != null){
svi.Center = droppingCursor.GetPosition(scatterView1);
svi.Orientation = droppingCursor.GetOrientation(scatterView1);
svi.Height = droppingCursor.Visual.ActualHeight;
svi.Width = droppingCursor.Visual.ActualWidth;
svi.SetRelativeZIndex(RelativeScatterViewZIndex.Topmost);
}
}
}
这一切都基本上来自sdk中的一个例子,我不记得其中哪一个很遗憾。
干杯,
Stian Farstad