将 MVVM 应用于填充了 UserControls 的 ItemsControl
当我用当前的应用程序解决越来越多与 MVVM 相关的问题时,更多的问题不断出现。 :)
我试图替换的当前实现涉及一个 StackPanel,其子级或多或少是动态生成的(通过查看配置文件)。每个子级都是 UserControl 的一个实例。之前,我所做的是为 StackPanel 分配一个名称,然后在 Window_Loaded 事件处理程序中,我只需确定所需的子级数量,为每个子级实例化一个 UserControl,并为 UserControl 分配一个 ID,以便我知道在特定 UserControl 实例上单击的按钮的正确来源;每个 UserControl 上都有 3 个按钮。
所以我知道我想将 StackPanel 绑定到一个集合。这当然是不可能的,因为我需要使用从 ItemsControl 派生的东西,例如 ListBox 或 ListView (甚至 ItemsControl 本身)。为了在 MVVM 化的第一次迭代中保持简单,我将只使用 ListBox。
现在的问题是,代码隐藏中的 ObservableCollection 应该是 ObservableCollection 吗?我相信这意味着无论我如何设置 GUI 的外观,此 ListBox 将始终具有其在 MyUserControl 的 XAML 文件中的外观的子项。我也希望它可以自定义,但我认为这意味着我也必须将 MVVM 模式应用到 UserControl。
As I knock off more and more MVVM-related issues with my current application, more just keep popping up. :)
The current implementation that I am trying to replace involves a StackPanel whose children are more or less dynamically generated (via looking in a configuration file). Each child is an instance of a UserControl. Before, what I did was assign a name to the StackPanel, and then in the Window_Loaded event handler, I'd simply determine the necessary number of children, instantiate one UserControl for each, and also assign the UserControl an ID so I'd know the proper source for the Buttons clicked on a particular UserControl instance; each UserControl has 3 buttons on it.
So I know I want to bind the StackPanel to a collection. This of course, is not possible, as I need to use something that derives from ItemsControl, like ListBox or ListView (or even ItemsControl itself). To keep it easy in the first iteration of MVVM-ifying, I'll just use a ListBox.
Now the question is, should my ObservableCollection in the code-behind be a ObservableCollection? I believe this means that no matter how I skin my GUI, this ListBox will always have children that look however they do in MyUserControl's XAML file. I'd like this to be customizable as well, but I assume this means I have to apply the MVVM pattern to the UserControl as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望每个列表项都有不同的模板,并且希望坚持 MVVM 风格,那么最好不要考虑 UserControls。
您可以让主视图将 ListBox 绑定到视图模型实例的可观察集合。如果您设置数据模板将 ViewModel 类映射到相应的 UserControl,则无需显式加载 UserControl - 只需将任何 ItemsControl 绑定到 ViewModel 集合,然后让数据模板负责构建并将其映射到该 VM 的正确用户控件。
If you want each of your list items to have different templates, and want to stick to an MVVM style, you're better off not thinking in terms of UserControls.
You can have your main view to have a ListBox bound to an observable collection of View Model instances. If you setup your data templates to map the ViewModel classes to their appropriate UserControl, you don't need to ever explicitly load UserControls - just bind any ItemsControl to your collection of ViewModels, and let the data templates take care of constructing and mapping this to the correct UserControl for that VM.
如果不需要的话,您的集合不必是
ObservableCollection
。 ObservableCollection 的“Observable”部分只是提供事件来通知其他人集合已更改,它与视觉表示无关。ObservableCollection
非常适合 MVVM,因为它提供了所有事件通知,但最终,无论您使用List
还是ObservableCollection
事物在任何给定时间点的视觉呈现方式都没有区别。Your collection doesn't have to be an
ObservableCollection
if it doesn't have to. The "Observable" part ofObservableCollection
is simply a matter of supplying events to notify others that the collection has changed, it is by no means related to visual representation.ObservableCollection
is very well suited for MVVM because of all the event notification it supplies, but ultimately, whether you use aList<T>
or anObservableCollection<T>
makes no difference in the way things appear visually at any given point in time.