[UWP]以编程方式在UWP ListView中移动项目而不刷新 [英] [UWP]programmatically move items in UWP ListView without refresh

查看:145
本文介绍了[UWP]以编程方式在UWP ListView中移动项目而不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题让我现在忙了很长一段时间,它似乎是如此基本,但它只是不起作用。归结为:

The following problem is keeping me busy now for quite some time, it seems to be so basic, yet it just doesn't work. It boils down to this:


  • 拥有一个绑定到某个合适集合的ListView代码隐藏(ObservableCollection,ReactiveList,或类似)
  • 我每隔x秒一次移动一个项目
  • ,所有这些项目得到刷新(至少看起来像那样。一瞬间所有项目都会消失,然后重新出现在新订单中)
  • Having a ListView that is bound to some suitable collection in code behind (ObservableCollection, ReactiveList, or alike)
  • I'm moving items one at a time every x seconds
  • on every move, all of the items get refreshed (at least it looks like that. for a split seconds all items disappear, then reappear in the new order)

必须有办法保留其他元素并且只是"移动"移动的项目。像拖放重新排序,但从后面的代码。我甚至不关心移动项目的花哨翻译动画,我只想让其他元素
留在屏幕上。当然,我的真实用例不是随机移动项目,而是在后面的代码中对列表进行排序。但我将问题追溯到这个简单的案例。

there must be a way to keep the other element and just "move" the moved item. Like the drag&drop reordering, but from the code behind. I even don't care about a fancy translation animation for the moved item, I just want the other elements to stay on screen. of course my real usecase is not moving items randomly but sorting the list in code behind. But I tracked the issue down to this simple case.

有一个简单的ListView并让它绑定例如到CodeBehind中的ObservableCollection,下面是我的虚拟代码来移动项目。没什么特别的,一切都归结为:每1秒将一个随机项目移动到索引5:

having a simple ListView and having it bound e.g. to a ObservableCollection in CodeBehind, the following is my dummy code to move around the items. Nothing special at all, it all boils down to: Move a random item to index 5 every 1 seconds:

_timer = new Timer(async _ =>
{
    Random r = new Random();
    var randomIndex = r.Next(0, contactsCvsSource.Count - 1);

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { contactsCvsSource.Move(randomIndex, 5); });
}
, null, 0, 1000);




推荐答案

你好,

ObservableCollection.Move调用RemoveItem的ObservableCollection实现,它导致刷新所有项目,而不是使用Collection< T>方法RemoveAt和Insert

ObservableCollection.Move call ObservableCollection implementation of RemoveItem which is cause to refresh all items, instead use Collection<T> methods RemoveAt and Insert

                        var s = contactsCvsSource[randomIndex];
                        contactsCvsSource.RemoveAt(randomIndex);
                        contactsCvsSource.Insert(5, s);


这篇关于[UWP]以编程方式在UWP ListView中移动项目而不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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