将 View 中的已排序 ListViewItems 同步到 ViewModel 中的 ObservableCollection

发布于 2024-10-01 19:13:30 字数 1054 浏览 4 评论 0原文

我使用遵循 MVVM 设计模式的 C# WPF。目前我的视图有一个 ListView ,其中包含多个列和行(即 ListViewItems )。

ViewModel 公开了 View 绑定到的 ObservableCollection 集合(如下所示)。

public ObservableCollection<Person> People
{
    get
    {
        return _people;
    }
    set
    {
        // new value?
        if( value != _people )
        {
            // Set
            _people = value;

            // Notify
            OnPropertyChanged( "People" );
        }
    }
}

这是绑定片段

<ListView ItemsSource="{Binding People}" ... >
    <!-- Omitted -->
</ListView>

在视图中,用户可以单击列标题,ListView 将相应地对其自身进行排序。问题在于 ViewModel 中的某些逻辑和命令对元素的顺序敏感,并且当 ListView 在特定列上排序时,底层 People 集合中的ViewModel 不反映用户所看到的顺序。

有关如何将 ListView 中排序的 ListViewItems 与底层 ObservableCollection 同步的任何线索?

编辑 已经在 ListView ItemsSource 上尝试过 Mode=TwoWay,但没有成功。

I'm using C# WPF following the MVVM design pattern. Currently my View has a ListView which contains several columns and rows ( i.e. ListViewItems ).

The ViewModel exposes an ObservableCollection<Person> collection ( as shown below ) which the View binds to.

public ObservableCollection<Person> People
{
    get
    {
        return _people;
    }
    set
    {
        // new value?
        if( value != _people )
        {
            // Set
            _people = value;

            // Notify
            OnPropertyChanged( "People" );
        }
    }
}

and here is the binding snippet

<ListView ItemsSource="{Binding People}" ... >
    <!-- Omitted -->
</ListView>

In the View the user may click on a column header and the ListView will sort itself accordingly. The problem is that some of the logic and commands in the ViewModel are sensitive to the order of elements and when the ListView is sorted on a particular column the underlying People collection in the ViewModel doesn't reflect the order the user is seeing.

Any clue on how to synchronize the sorted ListViewItems in the ListView with the underlying ObservableCollection<Person>?

Edit Already tried Mode=TwoWay on the ListView ItemsSource, that didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素罗衫 2024-10-08 19:13:30

问题是(正如您自己所说)ViewModel 中的某些逻辑和命令对元素的顺序敏感。简而言之,他们不应该这样;如果原因实际上是对项目进行重新排序(这几乎是我能想象的唯一一个顺序很重要的项目),那么视图不应该按列排序。否则,您需要找到使顺序无关的方法 - 也许您可以举一个取决于项目顺序的操作示例?这样我们就可以提出一个真正的解决方案。

还想澄清视图中项目的顺序与源 ObservableCollection 中项目的顺序无关。通常,ItemsControls 会自动创建一个独立排序的 ICollectionView。编辑:是的,这不是一个实际的 ICollectionView,它是一个更复杂的机制(ItemCollection 与 ItemContainerGenerator 结合使用),但你明白了。

The problem is (as you yourself stated) that some of the logic and commands in the ViewModel are sensitive to the order of elements. In short, they shouldn't be; if the reason is actually reordering the items (just about the only one I can imagine where the order would be important), then the view shouldn't be sortable by columns. Otherwise you need to find ways to make the order irrelevant - perhaps you could give an example of an action that depends on the order of items? That way we could suggest a real solution.

Also wanted to clarify that the order of the items in the view has no relation to their order in the source ObservableCollection. Generally the ItemsControls automatically create an ICollectionView that is sorted independently. Edit: yeah well not an actual ICollectionView, it's a more complicated mechanism (what the ItemCollection does in conjunction with the ItemContainerGenerator) but you get the picture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文