как использовать события RadDragAndDrop во ViewModel

Я использую telrik RadDragAndDrop Tool с ListBox. Я использую silverlight с mvvm light. Мой вопрос в том, как мне использовать этот код во ViewModel. Это код за файлом.

 public Construtor()
    {
        InitializeComponent();
        RadDragAndDropManager.AddDragQueryHandler(this, OnDragQuery);
        RadDragAndDropManager.AddDragInfoHandler(this, OnDragInfo);
        RadDragAndDropManager.AddDropQueryHandler(this, OnDropQuery);
        RadDragAndDropManager.AddDropInfoHandler(this, OnDropInfo);
    }
    private void OnDropInfo(object sender, DragDropEventArgs e)
    {
        ItemsControl box = e.Options.Destination as ItemsControl;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;

        if (e.Options.Status == DragStatus.DropComplete)
        {
            if (!itemsSource.Contains(section))
            {
                itemsSource.Add(section);
            }
        }
    }

    void OnDropQuery(object sender, DragDropQueryEventArgs e)
    {
        ItemsControl box = e.Options.Destination as ItemsControl;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;
        e.QueryResult = section != null && !itemsSource.Contains(section);
    }

    void OnDragInfo(object sender, DragDropEventArgs e)
    {
        ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
        ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;
        if (e.Options.Status == DragStatus.DragComplete)
        {
            if (section != null && itemsSource.Contains(section))
            {
                itemsSource.Remove(section);
            }
        }
    }

    protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)
    {
        ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
        ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
        if (e.Options.Status == DragStatus.DragQuery && box != null)
        {
            e.Options.Payload = box.SelectedItem;
            ContentControl cue = new ContentControl();
            cue.Content = box.SelectedItem;
            e.Options.DragCue = cue;
        }
        e.QueryResult = true;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SelectingQuestionsWindow window = new SelectingQuestionsWindow();
        window.Show();
        this.radExpander1.Visibility = System.Windows.Visibility.Visible;
    }

* XAML *> Это мой Xaml.

        <ListBox x:Name="SectionListBoxMain" Height="165" Width="200" SelectedItem="{Binding SelectedSectionList}"
                        DisplayMemberPath="SectionName" ItemsSource="{Binding SectionList}" ItemContainerStyle="{StaticResource draggableItemStyle}">

            <telerik:RadDragAndDropManager.AllowDrop>
                true
            </telerik:RadDragAndDropManager.AllowDrop>

        </ListBox>

        <TextBlock Width="20" />

        <ListBox x:Name="SectionListBox" Height="167" Width="200" ItemsSource="{Binding SelectedSectionList}" telerik:RadDragAndDropManager.AllowDrop="True" DisplayMemberPath="SectionName" ItemContainerStyle="{StaticResource draggableItemStyle}">

            </ListBox>

    </StackPanel>

person R76    schedule 10.02.2012    source источник


Ответы (1)


Это логика представления, которая на самом деле не принадлежит вашей ViewModel. Вероятно, это лучше подходит для Behavior.

См. Этот пример: http://www.telerik.com/help/silverlight/raddraganddrop-within-radgridview.html

Они используют поведение и прикрепляют его к сетке, чтобы разрешить переупорядочивание строк. Вы можете начать с чего-то вроде:

public partial class ListDragDropBehavior : Behavior<ListBox>

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

Затем вы можете использовать это поведение в других списках, просто прикрепив его к списку (я использую blend для добавления поведения)

person SalieHendricks    schedule 13.02.2012