Использование LibraryStacks в ScatterView на Surface

Мы пытаемся выяснить, как перетащить элемент из контейнера LibraryStack в ScatterView, например, как работают примеры приложений для просмотра фотографий. В настоящее время элемент просто возвращается в стек библиотек после того, как мы его вытаскиваем. Мы можем перетаскивать элементы в другие библиотечные стеки или библиотечные бары.

Вот пример того, что мы пытаемся сделать:

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

Спасибо!


person HappyCodeMonkey    schedule 27.10.2009    source источник


Ответы (2)


Это, безусловно, выполнимо. Я подготовил пример, который позволяет вам перетаскивать элементы из панели библиотеки, расположенной в окне рассеяния, и перетаскивать элементы в представление рассеяния, где они отображаются как новые элементы представления рассеяния.

Я не уверен, где вы ошиблись, но для того, чтобы перетаскивание работало, должно произойти следующее:

  1. Для цели перетаскивания должно быть установлено значение true для параметра AllowDrop.
  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

person Isak Savo    schedule 17.03.2010

Вам нужно установить фоновую кисть в окне рассеяния на цвет, т.е. прозрачный, чтобы он мог захватывать события перетаскивания.

вам также необходимо использовать SurfaceDragDrop.

SurfaceDragDrop.AddDropHandler (scatterView1, OnCursorDrop); AddHandler(ScatterViewItem.ScatterManipulationStartedEvent, новый 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 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);
    }
}

}

Это все в основном из примера в sdk, не помню какой из них к сожалению.

Ваше здоровье,

Стиан Фарстад

person Stian Farstad    schedule 15.03.2010