3

我们试图弄清楚如何将项目从 LibraryStack 容器拖到 ScatterView 上,就像照片查看器示例应用程序的工作方式一样。目前,项目只是在我们将其拖出后飞回 LibraryStack 中。我们可以将项目拖放到其他 LibraryStacks 或 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>

谢谢!

4

2 回答 2

3

这当然是可行的。我制作了一个示例,该示例允许您从位于 scatterview 内的 librarybar 中拖动项目并将项目拖放到 scatterview 上,它们显示为新的 scatterviewitems。

我不确定你哪里出错了,但为了使拖放工作,必须发生以下情况:

  1. 放置目标必须将 AllowDrop 设置为 true
  2. 放置目标必须对命中测试可见(通常通过设置除 null 以外的背景来完成 - 我使用透明)
  3. 放置目标必须处理 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

于 2010-03-17T10:22:11.647 回答
1

您需要将 scatterview 上的背景画笔设置为一种颜色,即对于它来说是透明的,甚至可以抓取放置事件。

您还需要使用 SurfaceDragDrop。

SurfaceDragDrop.AddDropHandler(scatterView1, OnCursorDrop); AddHandler(ScatterViewItem.ScatterManipulationStartedEvent, new ScatterManipulationStartedEventHandler(OnManipulationStarted));

private void OnManipulationStarted(object sender, RoutedEventArgs args)

{ ScatterViewItem svi = args.OriginalSource 作为 ScatterViewItem; if (svi != null)// && DragDropScatterView.GetAllowDrag(svi)) { svi.BeginDragDrop(svi.DataContext); } }

private void OnCursorDrop(object sender, SurfaceDragDropEventArgs args)

{ SurfaceDragCursor dropCursor = 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中的一个例子,我不记得是哪一个了。

干杯,

斯蒂安·法斯塔德

于 2010-03-15T20:57:09.163 回答