简体   繁体   English

在列表视图上拖放后如何在代码中更新ObservableCollection-WPF MVVM

[英]How to update ObservableCollection in codeBehind after drag and drop on listview - WPF MVVM

I am having some difficulty when I use the Josh Smith's issue for DnD on listView. 在listView上将Josh Smith的问题用于DnD时遇到了一些困难。

I have an ObservableCollection of "DetailTable" that I initialize in the ViewModel when I create the view : 创建视图时,我在ViewModel中初始化了一个“ DetailTable”的ObservableCollection:

(ListeTables is CollectionViewSource where I initialize and use my data) (ListeTables是CollectionViewSource,我在其中初始化和使用我的数据)

public ObservableCollection<DetailTable> ListeTablesDisplay
{
    get
    {
        var l = ListeTables.View.Cast<DetailTable>().ToList();
        return new ObservableCollection<DetailTable>(l);
    }
}

The listView in the Xaml file : Xaml文件中的listView:

    <ListView Name="ListeViewTables" SelectionMode="Single" 
              AllowDrop="true"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
              ItemsSource="{Binding ListeTablesDisplay, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
              SelectedItem="{Binding SelectedTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
              Margin="10,83,831,61">

And then I call the Class of Josh Smith for the DnD in the View CodeBehind : 然后在View CodeBehind中将DnD称为Josh Smith类:

public DataView()
{
    InitializeComponent();

    new ListViewDragDropManager<DetailTable>(this.ListeViewTables);
}

Until now, the drag and drop work correctly, but in the ViewModel the order of items of the ObservableCollection is not coherent with what I do in the View. 到目前为止,拖放工作正常,但是在ViewModel中,ObservableCollection的项顺序与我在View中所做的工作不一致。

Example : if I move the item-3 to the 5th position, it's ok with the View but when I debug I see the item-3 always to the 3rd position in my ObservableCollection. 示例:如果将item-3移到第5个位置,则可以使用View,但是当我调试时,我在ObservableCollection中总是将item-3移到第3个位置。

It's very problematic for what I want to do, I hope someone can help me ! 我要做的事情很麻烦,希望有人能帮助我!

Thanks 谢谢

If a person has the same problem, just add the event ProcessDrop on ListViewDragDropManager 如果一个人有同样的问题,只需在ListViewDragDropManager上添加事件ProcessDrop

Then in the viewModel the event need to move manually the item index in the ObservableCollection 然后在viewModel中,事件需要手动移动ObservableCollection中的项目索引

    public static void OnProcessDrop(object sender, ProcessDropEventArgs<T> e)
    {
        e.ItemsSource.Move(e.OldIndex, e.NewIndex);

        e.Effects = DragDropEffects.Move;
    }

Here an example by the autor of ListViewDragDropManager - "Custom drop logic" for help 这是ListViewDragDropManager的指导者的示例-“自定义放置逻辑”以寻求帮助

Here's how to handle this in the general case. 这是在一般情况下的处理方法。 OP was able to get it to work as desired with Smith's code by handling Smith's ProcessDrop event. 通过处理Smith的ProcessDrop事件,OP可以使它与Smith的代码一起工作。

In the drop handler, determine whether the target ListView's ItemsSource is null. 在放置处理程序中,确定目标ListView的ItemsSource是否为null。

If you have an ItemsSource , cast it to System.Collections.IList and do the reordering operation on the list , not on Items . 如果您有ItemsSource ,请将其ItemsSourceSystem.Collections.IList然后对列表 (而不是Items进行重新排序操作。 Forget Items in this branch. 忘记此分支中的Items If the items source is an ObservableCollection<T> , the ordering in the ListView will update. 如果项源是ObservableCollection<T> ,则ListView中的顺序将更新。 If it isn't, that's not your problem. 如果不是,那不是您的问题。 The consumer of your code who provided the wrong list type needs to post a question asking why WPF doesn't get change notifications from List<T> or int[] or String or whatever. 您的代码使用者提供了错误的列表类型,需要发布一个问题,询问为什么WPF无法从List<T>int[]String或任何其他内容获得更改通知。

If you don't have an ItemsSource , you just have ListViewItems. 如果没有ItemsSource ,则只有ListViewItems。 Reorder Items . 重新订购Items Easy. 简单。

I've never before seen Josh Smith's code , and he'd be the guy to talk to if you need updates. 我从未见过Josh Smith的代码 ,如果您需要更新,他将是可以与之交谈的人。 Personally, I use an Adorner rather than restyling the list control items, but you can make a case for either approach. 就个人而言,我使用的是Adorner而不是重新设置列表控件的样式,但是您可以为这两种方法辩护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM