使用Surface SDK 2“拖放”数据和模板

时间:2014-05-21 08:54:26

标签: c# wpf xaml pixelsense

我正在尝试在LibraryStackScatterView之间实施拖放操作。

我正在使用以下代码,该代码位于DragDropScatterView类中,该类是SDK随附的ShoppingCart示例的一部分 https://code.google.com/p/osuohiounionvirtualtour/source/browse/trunk/OhioUnionTour/DragDropScatterView.cs?r=10

  

private void OnCursorDrop(object sender,SurfaceDragDropEventArgs args)          {

       SurfaceDragCursor droppingCursor = args.Cursor;

       // Add dropping Item that was from another drag source.
       if (droppingCursor.CurrentTarget == scatterView && droppingCursor.DragSource != scatterView)
       {
           if (!scatterView.Items.Contains(droppingCursor.Data))
           {

               scatterView.Items.Add(droppingCursor.Data);

               var svi = scatterView.ItemContainerGenerator.ContainerFromItem(droppingCursor.Data) as ScatterViewItem;
               if (svi != null)
               {
                   svi.Center = droppingCursor.GetPosition(this);
                   svi.Orientation = droppingCursor.GetOrientation(this);
                   svi.Width = droppingCursor.Visual.ActualWidth;
                   svi.SetRelativeZIndex(RelativeScatterViewZIndex.Topmost);


               }
            }
       }
   }

这是我的XAML代码:

   <s:LibraryStack IsManipulationEnabled="False"  AllowDrop="True" >Name="listcategories" ItemsSource="{Binding}" Background="Black" >

       <s:LibraryStack.ItemTemplate>

           <DataTemplate>

       <UserControl Background="White"   AllowDrop="True" IsManipulationEnabled="True">
        <StackPanel AllowDrop="True" Orientation="Vertical">

                       <TextBlock AllowDrop="True" VerticalAlignment="Center" >HorizontalAlignment="Center"  Text="{Binding Name}"></TextBlock>
                       <Image  AllowDrop="True" VerticalAlignment="Center" >HorizontalAlignment="Center"  Height="Auto" Width="Auto"  Source="{Binding Picture}"   </Image> 


            </StackPanel>
       </UserControl>

           </DataTemplate>
       </s:LibraryStack.ItemTemplate>


  </s:LibraryStack>
     

拖放效果很好,但我只是拖动从UserControl中取出的LibraryStack的数据(因此我只将名称属于灰色方块)。

我想拖放整个UserControl,这意味着他的模板。

1 个答案:

答案 0 :(得分:1)

我设法解决了我的问题:

显然,当我进行拖放操作时,我的UserControl的数据信息也被拖放,但样式信息丢失了。

所以我在我的XAML文件中为我的Page.Resources和我的LibraryStack写了一个样式部分(在DragDropScatterView中)(作为SDK附带的ShoppingCart示例的一部分) 。两者都描述了与其物品完全相同的风格。

感谢下面的XAML代码,在拖放操作结束时再次找到样式信息:

    <!--Style for each data item on ScatterLayer and the cursor being dragged -->
    <Style x:Key="ScatterItemStyle" TargetType="{x:Type s:ScatterViewItem}">
        <Setter Property="Background" Value="{x:Null}" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <UserControl TouchDown="VisitCategory" Background="White" VerticalAlignment="Top"  AllowDrop="True" IsManipulationEnabled="True">

                        <StackPanel AllowDrop="True" Orientation="Vertical">

                            <TextBlock AllowDrop="True" VerticalAlignment="Center" HorizontalAlignment="Center"  Text="{Binding Name}"></TextBlock>
                            <Image  AllowDrop="True" VerticalAlignment="Center" HorizontalAlignment="Center"  Height="Auto" Width="Auto"  Source="{Binding Picture}"></Image>


                        </StackPanel>
                    </UserControl>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <!-- Style for the LibraryStack. -->
    <Style x:Key="StackStyle" TargetType="{x:Type s:LibraryStack}">
        <Setter Property="IsManipulationEnabled" Value="True" />
        <Setter Property="AllowDrop" Value="True" />
        <Setter Property="ItemsSource" Value="{Binding}" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type s:LibraryStackItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type s:LibraryStackItem}">
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <UserControl Background="White" VerticalAlignment="Top"  AllowDrop="True" IsManipulationEnabled="True">

                        <StackPanel AllowDrop="True" Orientation="Vertical">

                            <TextBlock AllowDrop="True" VerticalAlignment="Center" HorizontalAlignment="Center"  Text="{Binding Name}"></TextBlock>
                            <Image  AllowDrop="True" VerticalAlignment="Center" HorizontalAlignment="Center"  Height="Auto" Width="Auto"  Source="{Binding Picture}"></Image>


                        </StackPanel>
                    </UserControl>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Page.Resources>


<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="68" />
    </Grid.RowDefinitions>


        <loc:DragDropScatterView AllowDrop="True" Background="Black" 
    x:Name="scatterView" ItemContainerStyle="{StaticResource ScatterItemStyle}"/>



    <s:LibraryStack Grid.Row="1" Background="Black" Name="listcategories" Style="{DynamicResource StackStyle}" />

</Grid>