gpt4 book ai didi

c# - INotifyPropertyChanged 不会通过 View 模型

转载 作者:行者123 更新时间:2023-12-03 11:03:28 25 4
gpt4 key购买 nike

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

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在您绑定(bind)到的 View 模型上,否则绑定(bind)不知道何时更新。募集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);
}
}
}

关于c# - INotifyPropertyChanged 不会通过 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com