1

我正在使用telrik RadDragAndDrop工具和ListBox。我正在使用mvvm光的silverlight。我的問題是,我應該如何在ViewModel中使用此代碼。這是一個代碼背後的代碼。如何在ViewModel中使用RadDragAndDrop事件

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> 

回答

1

這個是並沒有真正屬於您的視圖模型視圖邏輯。它可能更適合行爲。

見這個例子:http://www.telerik.com/help/silverlight/raddraganddrop-within-radgridview.html

他們用行爲並將其連接到一個網格,使行重新排序。你可以從這樣的事情開始:

public partial class ListDragDropBehavior : Behavior<ListBox> 

你需要添加一些依賴項屬性來綁定到另一個列表框。

然後,您可以在其他列表框上使用此行爲,只需將其附加到列表框(我使用混合來附加行爲)

相關問題