在列表视图上拖放后如何在代码中更新ObservableCollection-WPF MVVM [英] How to update ObservableCollection in codeBehind after drag and drop on listview - WPF MVVM

查看:97
本文介绍了在列表视图上拖放后如何在代码中更新ObservableCollection-WPF MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在listView上将Josh Smith的问题用于DnD时,我遇到了一些困难.

I am having some difficulty when I use the Josh Smith's issue for DnD on listView.

我有一个"DetailTable"的ObservableCollection,在创建视图时在ViewModel中对其进行了初始化:

I have an ObservableCollection of "DetailTable" that I initialize in the ViewModel when I create the view :

(ListeTables是我在其中初始化和使用数据的CollectionViewSource)

(ListeTables is CollectionViewSource where I initialize and use my data)

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

Xaml文件中的listView:

The listView in the Xaml file :

    <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">

然后我在View CodeBehind中调用Dsh的Josh Smith类:

And then I call the Class of Josh Smith for the DnD in the View CodeBehind :

public DataView()
{
    InitializeComponent();

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

直到现在,拖放操作仍可以正常工作,但是在ViewModel中,ObservableCollection的项顺序与我在View中所做的操作不一致.

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.

示例:如果将item-3移至第5个位置,则可以使用View,但是当我调试时,我在ObservableCollection中始终将item-3移至第3个位置.

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.

这对我想做的事情很成问题,希望有人能帮助我!

It's very problematic for what I want to do, I hope someone can help me !

谢谢

推荐答案

如果某人遇到相同的问题,只需在ListViewDragDropManager

If a person has the same problem, just add the event ProcessDrop on ListViewDragDropManager

然后在viewModel中,该事件需要手动移动ObservableCollection中的项目索引

Then in the viewModel the event need to move manually the item index in the ObservableCollection

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

        e.Effects = DragDropEffects.Move;
    }

此处是一个示例ListViewDragDropManager的指导者-自定义放置逻辑"以获取帮助

这篇关于在列表视图上拖放后如何在代码中更新ObservableCollection-WPF MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆