Prism 中的 MVVM 多个视图同一个 ViewModel
我有一个关于具有相同视图模型类型的多个用户控件视图的问题。我似乎无法为我的困惑找到具体的答案,但这是相当推测的。
我有。
<StackPanel Orientation="Vertical">
<TextBlock Text="Signature Summary" FontSize="14" FontWeight="Bold" TextAlignment="Center" Height="30"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile1ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile2ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile3ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile4ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile5ViewModel}"/>
</StackPanel>
堆栈面板是主视图内的一个容器,它具有用于 Prism/Unity IoC 架构的依赖属性视图模型数据上下文。
因此,它们通过另一个接口将这些单独视图的数据连接绑定到主视图的接口的属性。
一切似乎都工作正常,并且 ParameterFileSummaryView 中的元素绑定很好地绑定到在第一个参数 ParamterFile1ViewModel 上设置的值。
这正是我想要的。但当然,这些 ViewModel 是在主窗口的 ViewModel 中构建的,而不是在 Unity 容器之外......这一切都感觉有点 hacky。有没有更干净的方法来实现我正在尝试的事情。
如果这真的是一个没有实际意义的问题,我很抱歉……但我只见树木不见森林。如果问题令人困惑,我将添加编辑,请耐心等待,我不是专家:)。
I have a question regarding multiple user controls views with the same view model type. I can't seems to find specific answers for my confusion but this is quite speculative.
I have.
<StackPanel Orientation="Vertical">
<TextBlock Text="Signature Summary" FontSize="14" FontWeight="Bold" TextAlignment="Center" Height="30"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile1ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile2ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile3ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile4ViewModel}"/>
<my:ParameterFileSummaryView DataContext="{Binding ParamterFile5ViewModel}"/>
</StackPanel>
the stack panel is a container inside a main view which has a dependancy property view model datacontext used for a Prism/Unity IoC architecture.
These are therefore binding the datacontect for these individual views to properties of the interface of the main view via another interface.
It all seems to work ok and the binding of the elements in the ParameterFileSummaryView bind nicely to the values set on the, say for the first one, ParamterFile1ViewModel.
Which is exactly what I want. But of cource these ViewModels are built within the ViewModel of the main window and not out of the Unity container.... It all feels a little bit hacky. Is there a cleaner way to implement what I am attempting.
Apologies if it is really a moot question... but I can't see the wood for the trees. If the question confuses I will add edits, please be patient I am not an expert :) .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要为主视图拉出正确的 ViewModel,请使用容器。作为服务定位器,让每个模型解析为相同的类型并拉出 IEnumerable,然后将其绑定到视图。
To pull the right ViewModel for the main view, use the container. Either as a service locator and have each model resolve to the same type and pull
IEnumerable<TViewModel>
then bind that to the view.