脱发和 MVVM 用户控件 [英] Hair loss and MVVM user controls

查看:11
本文介绍了脱发和 MVVM 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 编写的用户控件 &使用 MVVM 模式的 WPF.

I have a user control written in C# & WPF using the MVVM pattern.

我想要做的就是在绑定的 ViewModel 中有一个属性暴露在控件之外.我希望能够绑定到它,并且我希望对属性所做的任何更改都可以被绑定到公开值的控件之外的任何内容获取.

All I want to do is have a property in the bound ViewModel exposed to outside of the control. I want to be able to bind to it and I want any changes to the property to be picked up by anything outside the control that is bound to the exposed value.

这听起来很简单,但它让我拔掉了我的头发(剩下的不多).

This sounds simple, but its making me pull out my hair (and there is not much of that left).

我在用户控件中有一个依赖属性.ViewModel 具有实现 INotifyPropertyChanged 接口的属性,并且正在正确调用 PropertyChanged 事件.

I have a dependency property in the user control. The ViewModel has the property implementing the INotifyPropertyChanged interface and is calling the PropertyChanged event correctly.

一些问题:1) 如何在不破坏 MVVM 分离的情况下获取对 ViewModel 属性的更改并将其绑定到依赖属性?到目前为止,我设法做到这一点的唯一方法是在后面的控件代码中分配 ViewModels PropertyChanged 事件,这绝对不是 MVVM.

Some questions: 1) How do I pick up the changes to the ViewModel Property and tie it to the Dependency Property without breaking the MVVM separation? So far the only way I've managed to do this is to assign the ViewModels PropertyChanged Event in the Controls code behind, which is definitely not MVVM.

2) 使用上面的软糖,我可以让 Dependency 属性启动它的 PropertyChangedCallback,但是在控件之外绑定到它的任何东西都不会接受更改.

2) Using the above fudge, I can get the Dependency property to kick off its PropertyChangedCallback, but anything bound to it outside the control does not pick up the change.

必须有一种简单的方法来完成所有这些.请注意,我没有在这里发布任何代码 - 我希望不会影响我现有代码的答案.还有,反正你们可能都笑了...

There has to be a simple way to do all of this. Note that I've not posted any code here - I'm hoping not to influence the answers with my existing code. Also, you'd probably all laugh at it anyway...

罗布

好的,澄清一下 - 代码示例:

OK, to clarify - code examples:

背后的用户控制代码:

   public static DependencyProperty NewRepositoryRunProperty = DependencyProperty.Register("NewRepositoryRun", typeof(int?), typeof(GroupTree),
                                                                new FrameworkPropertyMetadata( null, new PropertyChangedCallback(OnNewRepositoryRunChanged)));
    public int? NewRepositoryRun
    {
        get { return (int?)GetValue(NewRepositoryRunProperty); }
        set
        {
            SetValue(NewRepositoryRunProperty, value);
        }
    }

    private static void OnNewRepositoryRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.OldValue != e.NewValue)
        {

        }
    }

    public GroupTree()
    {
        InitializeComponent();

        GroupTreeVM vm = new GroupTreeVM();

        this.DataContext = vm;

    }

视图模型(GroupTreeVM.cs)

Viewmodel (GroupTreeVM.cs)

   private int? _NewRepositoryRun;
    public int? NewRepositoryRun
    {
        get
        {
            return _NewRepositoryRun;
        }
        set
        {
            _NewRepositoryRun = value; 
            NotifyPropertyChanged();
        }
    }

推荐答案

现在我的每周不要那样做"回答...

And now for my weekly "don't do that" answer...

您遇到这个问题是因为那种气味,这应该表明您做错了什么.

You're experiencing this issue because of that smell, and it should be an indication that you're doing something wrong.

解决方案是放弃为 UserControl 构建的 VM.如果它包含业务逻辑,则应将其移至另一个 ViewModel 中的适当位置.

The solution is to ditch the VM built for the UserControl. If it contains business logic, it should be moved to an appropriate location in another ViewModel.

您应该认为 UserControl 只不过是一个更复杂的控件.TextBox 是否有自己的 ViewModel?不可以.您将 VM 的属性绑定到控件的 Text 属性,控件会在其 UI 中显示您的文本.

You should think of a UserControl as nothing more than a more complex control. Does the TextBox have its own ViewModel? No. You bind your VM's property to the Text property of the control, and the control shows your text in its UI.

像这样考虑 MVVM 中的 UserControl--对于每个模型,您都有一个 UserControl,它旨在向用户展示该模型中的数据.您可以在任何想要向用户显示该模型的地方使用它.它需要一个按钮吗?在您的 UserControl 上公开一个 ICommand 属性,并让您的业务逻辑绑定到它.您的业​​务逻辑是否需要了解内部发生的事情?添加路由事件.

Think of UserControls in MVVM like this--For each model, you have a UserControl, and it is designed to present the data in that model to the user. You can use it anywhere you want to show the user that model. Does it need a button? Expose an ICommand property on your UserControl and let your business logic bind to it. Does your business logic need to know something going on inside? Add a routed event.

通常,在 WPF 中,如果您发现自己问为什么做某事会很痛,那是因为您不应该这样做.

Normally, in WPF, if you find yourself asking why it hurts to do something, it's because you shouldn't do it.

这篇关于脱发和 MVVM 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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