WPF和MVVM:如何刷新控件
这是我第一个使用WPF、MVVM和EntityFramework6代码的应用程序。它是一个简单的小型信用模拟器,由包含信用参数的左面板和右面板中刷新参数每次更改的datagrid组成,它包含实体Echeance的集合。因此,左侧面板包含数据索引到Simulation Entity中属性的文本框,并且数据网格数据索引到ObservableCollection 问题是,当我更改任何参数时,datagrid不会刷新更改 在使用MVVM之前,应用程序运行良好。 代码如下:WPF和MVVM:如何刷新控件,wpf,xaml,mvvm,datagrid,Wpf,Xaml,Mvvm,Datagrid,这是我第一个使用WPF、MVVM和EntityFramework6代码的应用程序。它是一个简单的小型信用模拟器,由包含信用参数的左面板和右面板中刷新参数每次更改的datagrid组成,它包含实体Echeance的集合。因此,左侧面板包含数据索引到Simulation Entity中属性的文本框,并且数据网格数据索引到ObservableCollection 问题是,当我更改任何参数时,datagrid不会刷新更改 在使用MVVM之前,应用程序运行良好。 代码如下: //Entity Echean
//Entity Echeance
public partial class Echeance : INotifyPropertyChanged
{
public long echeanceId { get; set; }
public byte echNumber { get; set; }
public double principal;
.... //Other fields
...
//Navigation to the parent
public virtual simulation simulation { get; set; }
//Contructor with Echeance Number
Echeance(byte n)
{
echNumber = n;
}
...
public double MontantPrincipal
{
get
{
return principal;
}
set
{
principal = value;
OnPropertyChanged("MontantPrincipal");
}
}
...Other properties
....
//
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
//Entity simulation
public partial class simulation
{
public long simulationId { get; set; }
...
public double loyer { get; set; }
public virtual IList<Echeance> echeancier { get; set; }
}
datagrid不会刷新ObservableCollection,Loyer属性也不会刷新。我调试了这个命令,发现该命令运行良好,列表包含正确的数据,但并没有刷新。
当我单击任何列标题时,datagrid中的数据将正确刷新。奇怪的行为
提前感谢您通过将值设置到字段,您将绕过属性设置器和INotifyPropertyChange触发器,以便在ctor中将值设置为属性而不是字段 如果VM的构造函数:
#region Constructeur
public VMSimulation()
{
_simulation = new simulation();
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier); // instead of _echeancier = ...
// LogIt();
}
#endregion
对不起,ViewModel中的命令代码缺失
public ICommand RefreshCommand
{
get
{
if (_refreshCommand == null)
_refreshCommand = new DelegateCommand<object>(RefreshCommandExecute);
return _refreshCommand;
}
}
void RefreshCommandExecute(object obj)
{
_simulation.RefreshEcheancier();
//I added this line and it works fine
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier);
}
因此,我最近添加了一行:
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier);
datagrid刷新工作正常
谢谢…对于更新视图模型中所做更改的UI控件,视图模型和数据对象类必须实现INotifyPropertyChanged接口。看起来您已经这样做了,但我不能确定您的实现是否有效。请尝试ItemsSource={Binding Echeancier,UpdateSourceTrigger=PropertyChanged},看看这是否有帮助。我尝试过了…仍然是相同的问题。您能澄清一下吗?您的意思是我必须从INotifyPropertyChanged继承模拟吗?通过将值设置为字段,您绕过了Echeancier属性设置器,从而绕过了OnPropertyChangedEcheancier;从未调用过,VM也没有机会报告更改。user3455395:我写的是正确的。请查看我上面的代码。我写道:_echeancier=newobserveablecollection_simulation.echeancier;Not Echeancier=新的可观测采集_simulation.Echeancier;您需要设置属性才能使绑定正常工作。只需尝试更新您的构造函数以使用属性,然后看看它是否有效。
#region Constructeur
public VMSimulation()
{
_simulation = new simulation();
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier); // instead of _echeancier = ...
// LogIt();
}
#endregion
public ICommand RefreshCommand
{
get
{
if (_refreshCommand == null)
_refreshCommand = new DelegateCommand<object>(RefreshCommandExecute);
return _refreshCommand;
}
}
void RefreshCommandExecute(object obj)
{
_simulation.RefreshEcheancier();
//I added this line and it works fine
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier);
}
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier);