如何支持列表框SelectedItems在通航与应用结合MVVM [英] How to support ListBox SelectedItems binding with MVVM in a navigable application

查看:110
本文介绍了如何支持列表框SelectedItems在通航与应用结合MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个WPF应用程序,通过自定义下一步,通航后退按钮和命令(即不使用 NavigationWindow )。在一个屏幕上,我有一个的ListBox 有支持多种选择(使用扩展模式)。我有此屏幕的图模型和所选择的项存储为属性,因为它们需要维护。

I am making a WPF application that is navigable via custom "Next" and "Back" buttons and commands (i.e. not using a NavigationWindow). On one screen, I have a ListBox that has to support multiple selections (using the Extended mode). I have a view model for this screen and store the selected items as a property, since they need to be maintained.

不过,我知道,一个 ListBox的 SelectedItems 属性是只读的。我一直在尝试使用 href=\"http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html\">这个解决方案来解决这个问题,但我一直无法将其采纳进我的实现。我发现,当一个或多个元素被取消,我不能区分,当我浏览屏幕之间( NotifyCollectionChangedAction.Remove 升起在这两种情况下,由于技术上的所有选定导航远离屏幕时,项目被取消)。我的导航命令位于其中管理每个屏幕的视图模型一个单独的视图模式,所以我不能把涉及与的ListBox 在那里视图模型的任何实现

However, I am aware that the SelectedItems property of a ListBox is read-only. I have been trying to work around the issue using this solution here, but I have not been able to adopt it into my implementation. I found that I can't differentiate between when one or more elements are deselected and when I navigate between screens (NotifyCollectionChangedAction.Remove is raised in both cases, since technically all the selected items are deselected when navigating away from the screen). My navigation commands are located in a separate view model which manages the view models for each screen, so I can't put any implementation related to the view model with the ListBox in there.

我发现其他几个不太优雅的解决方案,但没有这些似乎强制执行双向视图模型和视图之间的结合。

I have found several other less elegant solutions, but none of these seem to enforce a two-way binding between the view model and the view.

任何帮助将大大AP preciated。我可以提供一些我的源$ C ​​$ C,如果它有助于了解我的问题。

Any help would be greatly appreciated. I can provide some of my source code if it would help to understand my problem.

推荐答案

尝试在每个数据项创建一个 IsSelected 财产和有约束力的 ListBoxItem的.IsSelected 来的财产

Try creating an IsSelected property on each of your data items and binding ListBoxItem.IsSelected to that property

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>

这篇关于如何支持列表框SelectedItems在通航与应用结合MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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