2009-10-27 56 views
3

我們試圖弄清楚如何將LibraryStack容器中的項目拖動到ScatterView上,例如照片查看器示例應用程序的工作方式。目前,該項目在我們將其拖出之後會飛回到LibraryStack。我們可以將項目拖放到其他LibraryStack或LibraryBars中。在Surface上的ScatterView中使用LibraryStacks

這裏是我們試圖什麼樣:

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

謝謝!

回答

3

這肯定是可行的。我製作了一個示例,允許您從位於散點視圖內的庫欄拖動項目,並將它們作爲新的散點視圖項目顯示在散點視圖中。

我不知道你在哪裏錯了,但爲了拖/放工作,下面有發生:

  1. 放置目標必須具備的AllowDrop設置爲true
  2. 的下降目標必須是可見的命中測試(通常是通過設置以外的背景不是空來完成 - 我使用透明)
  3. 放置目標必須處理丟棄事件和做一些巧妙的與數據

這是我的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); 
} 

顯然,上面的代碼沒有處理拖動項目回librarybar。我將它作爲一個練習,讀者;-)

以下MSDN指導是我絕對認爲你應該閱讀:http://msdn.microsoft.com/en-us/library/ee804812.aspx

+0

非常有幫助,謝謝! – jackbot 2010-04-30 11:51:30

1

您需要將散點圖上的背景刷設置爲一種顏色,即透明 ,因爲它甚至可以抓取放置事件。

您還需要使用SurfaceDragDrop。

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

private void OnManipulationStarted(object sender, RoutedEventArgs args) 

{ ScatterViewItem SVI = args.OriginalSource作爲ScatterViewItem; (svi!= null)// & & 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); 
    } 
} 

}

這是所有basicly從SDK的一個例子,我不記得這其中的一個很可悲的。

乾杯,

了Stian法斯塔德