Я пытаюсь реализовать операцию перетаскивания между LibraryStack
и ScatterView
.
Я использую следующий код, найденный в классе DragDropScatterView
который является частью образца ShoppingCart, который поставляется с SDK на странице https://code.google.com/p/osuohiounionvirtualtour/source/browse/trunk/OhioUnionTour/DragDropScatterView. cs? r = 10:
private void OnCursorDrop (отправитель объекта, 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
(так что я получил атрибут Name в сером квадрате).
Я хотел бы перетащить весь UserControl, что означает его шаблон.
Мне удалось решить мою проблему:
Очевидно, что когда я выполнял операцию перетаскивания, информация о данных моего UserControl
была перетаскивана и потеряна, но информация о стиле была потеряна.
Поэтому я написал в своем файле XAML раздел стиля (в Page.Resources
) для моего LibraryStack
и моего DragDropScatterView
(класс, который является частью образца ShoppingCart, который поставляется с SDK). Оба описывают точно такой же стиль для своих предметов.
Благодаря приведенному ниже коде 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>