具有当前项目的可观察集合?
在我的应用程序的对话框中,我有一个绑定到选项卡列表的可观察集合(存储在其他地方)。当我关闭并重新打开对话框时,当前选定的选项卡会丢失并最终成为第一个选项卡。如何设置选项卡以便保留所选选项卡?
我的印象是可观察集合具有“当前项目”属性,但事实似乎并非如此。我查看了 ItemCollection 和 CollectionView,它们确实具有当前项目属性,但我不确定这是否是我感兴趣的。
In a dialog from my application, I have an observable collection (stored somewhere else) bound to a list of tabs. When I close and reopen the dialog, the currently selected tab gets lost and winds up to the be the first one. How do I set up my tabs so that the selected tab persists?
I had the impression that the observable collection had "current item" property, but that doesn't seem to be case. I've looked at ItemCollection and CollectionView, which do have a current item property, but I'm not sure if that's that I'd be interested in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个老问题,但我会回答那些可能正在寻找这个问题的人...
我使用 MVVM 模式处理这个问题的方法是在我的 ViewModel 上同时拥有 TabControl 绑定其 ItemsSource 的 ObservableCollection 和 ActiveItem TabControl 将其 SelectedItem 属性绑定到的属性。
它工作得很好并且保留了 MVVM 的设计原则(即 ViewModel 不应该知道视图)。
This is an old question, but I will answer for those might be looking for this...
The way I handled this using the MVVM pattern was to have on my ViewModel both an ObservableCollection to which the TabControl binds its ItemsSource as well as an ActiveItem property to which the TabControl binds its SelectedItem property.
It works pretty well and keeps the design principles of MVVM (ie, the ViewModel should not be aware of the view).
在对话框的
Closing
事件中,保存当前选定的TabItem
。在对话框的
Loaded
事件中,只需说:代码隐藏是实现此目的的最简单方法。
ObservableCollection
不会帮助你。In the
Closing
event of your dialog, save away the currently selectedTabItem
.In the
Loaded
event of your dialog, simply say:Code-behind is the easiest way to accomplish this.
ObservableCollection
is not going to help you.