2011-04-06 64 views
1

我有2個列表框(ListA和ListB),顯示來自不同實體(EntityA,EntityB)的數據。這些實體是相關的 - EntityA具有EntityB集合的屬性。我希望能夠使用拖放行爲將ListB中的項目添加到ListA中丟棄項目的集合中。Silverlight 4 ListBoxDragDropTarget放到ListBoxItem

爲了說明問題,我不想將ListItemB添加到所選ListItemA的集合中,我想將它添加到列表項的集合中,並將其放到(ListItemA,鼠標停留在發佈)。

使用ListBoxDragDropTarget,ListBoxItem可能是放置目標,而不是列表框本身?

對此方案的解決方案有何建議?

回答

1

可以通過創建兩個ListBox來完成,如上所述,一個綁定到ObservableCollection<EntityA>,另一個綁定到ObservableCollection<EntityB>。 EntityA包含一個ObservableCollection<EntityB>作爲一個屬性。 EntityA的列表框項目被模板化爲將EntityB的子集合顯示爲列表框。 ListBoxDragDropTarget在此ItemTemplate中指定,而不是父級。下面是一些XAML證明:

<ListBox Name="listOfEntityA"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding EntityName}" /> 
        <toolKit:ListBoxDragDropTarget AllowDrop="True" AllowedSourceEffects="All"> 
         <ListBox ItemsSource="{Binding ChildEntityBs}"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding EntityName}" /> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
        </toolKit:ListBoxDragDropTarget> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <toolKit:ListBoxDragDropTarget AllowDrop="True" AllowedSourceEffects="All"> 
     <ListBox Name="listOfEntityB" /> 
    </toolKit:ListBoxDragDropTarget> 
+0

可能是我沒有好好解釋一下自己。我想利斯塔顯示列表和數組listB顯示列表,但是當ListItemA拖動到ListItemB,EntityA應該添加到勢必ListItemB – 2011-04-07 00:32:44

+0

不用擔心EntityB財產清單。感謝您的澄清。 – topspin 2011-04-07 02:37:48

1

一些工作我想後,我知道了:

<StackPanel Orientation="Horizontal"> 
      <Controls:ListBoxDragDropTarget AllowDrop="True"> 
       <ListBox x:Name="FromBox" Width="200" ItemsSource="{Binding IssueList}" DisplayMemberPath="Name"/> 
      </Controls:ListBoxDragDropTarget> 

      <Controls:ListBoxDragDropTarget AllowDrop="True" Drop="ToBoxDragDropTarget_Drop"> 
       <ListBox x:Name="ToBox" Width="150" ItemsSource="{Binding ObjectiveList}" DisplayMemberPath="Name" Margin="80,0,0,0" /> 
      </Controls:ListBoxDragDropTarget> 

      <TextBlock x:Name="UpdateText"/> 
     </StackPanel> 

和代碼隱藏(現在將重構爲我的視圖模型):

public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent();    
      IssueList = new ObservableCollection<Issue> 
                { 
                 new Issue{ ID = 1, Name="One"}, 
                 new Issue{ ID = 2, Name="Two"}, 
                 new Issue{ ID = 3, Name="Three"}, 
                 new Issue{ ID = 4, Name="Four"}, 
                 new Issue{ ID = 5, Name="Five"}, 
                }; 
      ObjectiveList = new ObservableCollection<Objective> 
                  { 
                   new Objective {ID = 10, Name = "Ten"}, 
                   new Objective {ID = 11, Name = "Eleven"}, 
                   new Objective {ID = 12, Name = "Twelve"}, 
                   new Objective {ID = 13, Name = "Thirteen"}, 
                   new Objective {ID = 14, Name = "Fourteen"}, 
                  }; 

      LayoutRoot.DataContext = this; 
     } 

     public ObservableCollection<Issue> IssueList { get; set; } 
     public ObservableCollection<Objective> ObjectiveList { get; set; } 

     private void ToBoxDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e) 
     { 
      var droppedOnObjective = ((FrameworkElement)e.OriginalSource).DataContext as Objective; 
      var args = e.Data.GetData(typeof(ItemDragEventArgs)) as ItemDragEventArgs; 
      if (args != null) 
      { 
       var draggedItems = args.Data as SelectionCollection; 
       var draggedItem = draggedItems[0]; 
       if (droppedOnObjective != null) 
       { 
        var draggedIssue = (Issue)draggedItem.Item; 


        if (!droppedOnObjective.Issues.Contains(draggedIssue)) 
        { 
         droppedOnObjective.Issues.Add(draggedIssue); 
         UpdateText.Text = string.Format("Issue <{0}> added to Objective <{1}>", draggedIssue.Name, droppedOnObjective.Name); 
        } 
        else 
        { 
         UpdateText.Text = string.Format("Objective <{0}> already contains Issue <{1}>", droppedOnObjective.Name, draggedIssue.Name); 
        }      
       } 
       else 
        UpdateText.Text = "selections or dropOnObjective is null"; 
      } 
      else 
       UpdateText.Text = "args null"; 
     } 
    } 

    public class Issue 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 

    } 

    public class Objective 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public List<Issue> Issues { get; set; } 

     public Objective() 
     { 
      Issues = new List<Issue>(); 
     }   
    }