MVVM从viewModel访问其他视图的元素

发布于 2024-12-05 18:11:16 字数 232 浏览 1 评论 0原文

我开始在一个新项目中使用 MVVM 模式。 一切都很好,但我遇到了以下问题。 实现如下所示: 我有一个 MainView,主应用程序窗口。在此窗口中,我有一个 telerik RadGroupPanel,其中我将其余的应用程序视图托管为选项卡。 其余的 viewModel 不知道托管在 MainVIew 中的 RadGroupPanel。 我应该如何从 viewModels 中的命令正确地将这些视图添加到 RadGroupPanel 中? 谢谢。

I started working with the MVVM pattern in a new project.
Everything is ok, but i came to the following problem.
The implementation looks like this:
I have a MainView, the main app window. In this window i have a telerik RadGroupPanel in wich I host the rest of the app views as tabs.
The rest of the viewModels does not know about this RadGroupPanel which is hosted in MainVIew.
How should i correctly add those views to the RadGroupPanel from the commands in the viewModels?
Thanks.

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

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

发布评论

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

评论(2

泪是无色的血 2024-12-12 18:11:16

您是否考虑过使用接口将视图注入到 ViewModel 中以保持分离?我知道这会破坏 MVVM,但我已经在许多 WPF 项目中成功使用了它。我称之为 MiVVM或模型接口到视图ViewModel

图案很简单。您的用户控件应该有一个界面,称之为 IView。然后在 ViewModel 中你有一个带有 IMyView 类型的 setter 的属性,比如

public IMyView InjectedView { set { _injectedView = value; } }

然后在视图中创建一个名为 This 的依赖属性,

public MyUserControl : IMyView
{
    public static readonly DependencyProperty ThisProperty = 
         DependencyProperty.Register("This", typeof(IMyView), typeof(MyUserControl)); 

    public MyUserControl() 
    {
       SetValue(ThisProperty, this);
    } 
    public IMyView This { get { return GetValue(ThisProperty); } set { /* do nothing */ } } 
}

最后在 Xaml 中你可以使用绑定将视图直接注入到 ViewModel 中

<MyUserControl This="{Binding InjectedView, Mode=OneWayToSource}"/>

尝试一下出去!我已经多次使用这种模式,并且您可以在启动时获得注入一次视图的接口。这意味着您可以保持分离(可以测试 Viewmodel,因为可以模拟 IView),但您可以解决许多第三方控件缺乏绑定支持的问题。另外,它的速度很快。您知道绑定使用反射吗?

上面的博客链接中有一个演示项目展示了这种模式。如果您使用第三方控件,我建议尝试 MiVVM 的附加属性实现。

Have you considered injecting your view into the ViewModel using an interface to maintain separation? I know this breaks MVVM but I've successfully used this on a number of WPF projects. I call it MiVVM or Model Interface-to-View ViewModel.

The pattern is simple. Your Usercontrol should have an interface, call it IView. Then in the ViewModel you have a property with a setter of type IMyView, say

public IMyView InjectedView { set { _injectedView = value; } }

Then in the view you create a dependency property called This

public MyUserControl : IMyView
{
    public static readonly DependencyProperty ThisProperty = 
         DependencyProperty.Register("This", typeof(IMyView), typeof(MyUserControl)); 

    public MyUserControl() 
    {
       SetValue(ThisProperty, this);
    } 
    public IMyView This { get { return GetValue(ThisProperty); } set { /* do nothing */ } } 
}

finally in Xaml you can inject the view directly into the ViewModel using binding

<MyUserControl This="{Binding InjectedView, Mode=OneWayToSource}"/>

Try it out! I've used this pattern many times and you get an interface to the view injected once on startup. This means you maintain separation (Viewmodel can be tested as IView can be mocked), yet you get around the lack of binding support in many third party controls. Plus, its fast. Did you know binding uses reflection?

There's a demo project showcasing this pattern on the blog link above. I'd advocate trying out the Attached Property implementation of MiVVM if you are using a third party control.

仅一夜美梦 2024-12-12 18:11:16

You can have the list of viewmodels that you need to add controls for in an ObservableCollection in your main window viewmodel. You can then bind the ItemsSource of the RadGroupPanel to that collection and use the ItemTemplateSelector and ContentTemplateSelector of the RadGroupPanel to select the right template to use based on the viewmodel that is bound.

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