Silverlight 4 ListBoxDragDropTarget放到ListBoxItem上

时间:2011-04-06 02:46:24

标签: silverlight drag-and-drop listbox

我有2个列表框(ListA和ListB),显示来自不同实体(EntityA,EntityB)的数据。这些实体是相关的 - EntityA具有EntityB集合的属性。我希望能够使用拖放行为,以便将ListB中的项目添加到ListA中已删除项目的集合中。

为了澄清,我不想将ListItemB添加到所选ListItemA的集合中,我想将它添加到我将其放入的列表项的集合中(当我发布时鼠标结束的ListItemA) )。

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

有关此方案的解决方案的任何建议吗?

2 个答案:

答案 0 :(得分:1)

可以通过创建两个ListBox来完成,如您所述,一个绑定到ObservableCollection<EntityA>,一个绑定到ObservableCollection<EntityB>。 EntityA包含ObservableCollection<EntityB>作为属性。 EntityA的ListBox项目被模板化以将EntityB的子集合显示为ListBox。 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>

答案 1 :(得分: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>

和代码隐藏(现在将重构为我的ViewModel):

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>();
        }        
    }