INotifyPropertyChanged 不会通过视图模型
INotifyPropertyChanged wont pass through View Model
我正在制作一个 WinForms 程序,其架构模式与 MVVM 非常相似。主要区别在于 class 我用作模型,也用作用户控件。我知道这可能不是最棒的设置,但这是我必须使用的设置。
The problem is, that when the property in the model is changed, the change isn't reflected in the view...
我怀疑我没有正确实施 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));
}
}
您需要在绑定到的视图模型上引发 PropertyChanged
,否则绑定将不知道何时更新。要在模型更改时在 FormattedPrice
属性 上提高 PropertyChanged
,您可以这样做。
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);
}
}
}
我正在制作一个 WinForms 程序,其架构模式与 MVVM 非常相似。主要区别在于 class 我用作模型,也用作用户控件。我知道这可能不是最棒的设置,但这是我必须使用的设置。
The problem is, that when the property in the model is changed, the change isn't reflected in the view...
我怀疑我没有正确实施 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));
}
}
您需要在绑定到的视图模型上引发 PropertyChanged
,否则绑定将不知道何时更新。要在模型更改时在 FormattedPrice
属性 上提高 PropertyChanged
,您可以这样做。
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);
}
}
}