INotifyPropertyChanged不会通过View Model

时间:2017-05-02 05:45:12

标签: c# winforms mvvm inotifypropertychanged

我正在制作一个WinForms程序,其架构模式与MVVM非常相似。主要区别在于我使用的类作为模型,也充当用户控件。我知道这可能不是最棒的设置,但这是我必须使用的。

  

问题是,当模型中的属性发生变化时,变化不会反映在视图中......

我怀疑我没有正确实施INotifyPropertyChanged,但我确实看不到任何错误。我希望你们能......

模型

public partial class ModelAndUserControl : UserControl, INotifyPropertyChanged
{
    private decimal _price;

    public ModelAndUserControl()
    {
        InitializeComponent();
    }

    // Implement INotifyPropertyChanged        
    public event PropertyChangedEventHandler PropertyChanged;
    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    // Changes in this property, should be reflected in the view
    public decimal Price
    {
        get { return _price; }
        set
        {
            _price = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("Price");
        }
    }
}

查看-模型

public class MyViewModel
{
    private readonly MyView view;
    private ModelAndUserControl model;

    public MyViewModel(MyView view, ModelAndUserControl model) 
    {
        this.view = view;
        this.model = model;
    }

    // Debugging reveals that the value of the formatted 
    // string, changes correctly when the model property changes.
    // But the change isn't reflected in the view.
    public string FormattedPrice
    {
        get { return string.format("{0:n0} EUR", model.Price); }
    }
}

查看

public partial class MyView : UserControl
{
    private MyViewModel viewModel;

    public MyView() 
    {
        InitializeComponent(ModelAndUserConrol model);

        // Create an instance of the view model
        viewModel = new MyViewModel(this, model);

        // Create the data binding to the price
        txtPrice.DataBindings.Add("Text", viewModel, nameof(viewModel.FormattedPrice));
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在绑定的视图模型上引发PropertyChanged,或者绑定不知道何时更新。要在模型更改时在PropertyChanged属性上提升FormattedPrice,您可以执行以下操作。

public class MyViewModel : INotifyPropertyChanged
{
    private readonly MyView view;
    private ModelAndUserControl model;

    public MyViewModel(MyView view, ModelAndUserControl model) 
    {
        this.view = view;
        this.model = model;
        this.model.PropertyChanged += Model_PropertyChanged;
    }

    // Debugging reveals that the value of the formatted 
    // string, changes correctly when the model property changes.
    // But the change isn't reflected in the view.
    public string FormattedPrice
    {
        get { return string.format("{0:n0} EUR", model.Price); }
    }

    private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch(e.PropertyName){
            case "Price":
                InvokePropertyChanged("FormattedPrice");
                break;
            default:
                break;
        }
    }

    // Implement INotifyPropertyChanged        
    public event PropertyChangedEventHandler PropertyChanged;
    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
}