【问题标题】:Using LibraryStacks in a ScatterView on Surface在 Surface 上的 ScatterView 中使用 LibraryStacks
【发布时间】:2010-11-05 23:58:09
【问题描述】:

我们正在尝试弄清楚如何将项目从 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>

谢谢!

【问题讨论】:

    标签: c# wpf pixelsense


    【解决方案1】:

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

    我不确定你哪里出错了,但是为了使拖放工作,必须进行以下操作:

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

    【讨论】:

      【解决方案2】:

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

      您还需要使用 SurfaceDragDrop。

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

      private void OnManipulationStarted(object sender, RoutedEventArgs args)
      

      { ScatterViewItem svi = args.OriginalSource as 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中的一个例子,遗憾的是我不记得是哪一个了。

      干杯,

      斯蒂安·法斯塔德

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多