这是我第一个使用 WPF、MVVM 和 Entity Framework 6 Code First 的应用程序。它是一个简单的迷你信用模拟器,由包含信用参数的左侧面板和右侧面板中的数据网格组成,用于刷新参数中所做的每次更改,它包含实体“Echeance”的集合。因此,左侧面板包含文本框数据绑定(bind)到“模拟”实体中的属性,数据网格数据绑定(bind)到 ObservableCollection。
问题是,当我更改任何参数时,数据网格不会刷新更改。
在我使用 MVVM 之前,该应用程序运行良好。
代码下方:
//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; }
}
View 模型如下:
public class VMSimulation : ObservableObject
{
#region Fields
simulation _simulation;
...
ObservableCollection<Echeance> _echeancier;
#endregion
#region Constructeur
public VMSimulation()
{
_simulation = new simulation();
_echeancier = new ObservableCollection<Echeance>(_simulation.echeancier);
// LogIt();
}
#endregion
#region Properties
public ObservableCollection<Echeance> Echeancier
{
get
{
return _echeancier;
}
set
{
_echeancier = value;
OnPropertyChanged("Echeancier");
}
}
....
public double Loyer
{
get { return _simulation.loyer; }
set
{
_simulation.loyer = value;
OnPropertyChanged("Loyer");
}
}
...
}
XAML 只是我有刷新问题的字段
<viblend:NumberEditor x:Name="txloy"
Value="{Binding Path=Loyer, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
Grid.Column="7" Grid.Row="2"
Style="{StaticResource viBlendDecimal}" Width="72" ToolTip="Loyer computed." IsEnabled="False" />
<DataGrid x:Name="gridLoyers" ItemsSource="{Binding Echeancier}"
AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="0"
Grid.Column="0" Grid.Row="1" CellEditEnding="gridLoyers_CellEditEnding_1" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding NumLoy, Mode=TwoWay, StringFormat='{}{0:#}'}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="N°" />
<DataGridTextColumn Binding="{Binding DateEcheance , StringFormat={}\{0:dd/MM/yyyy\}, Mode=TwoWay}" ElementStyle="{StaticResource DataGridCellCenterAlignment}" Header="Echéance"/>
<DataGridTextColumn Binding="{Binding MontantPrincipal, StringFormat='{}{0:#.##,0}',UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="Principal" />
<DataGridTextColumn Binding="{Binding MontantInteret, StringFormat='{}{0:#.##,0}'}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="Intérêts"/>
<DataGridTextColumn Binding="{Binding MontantHT, StringFormat='{}{0:#.##,0}', UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="Hors taxe" />
<DataGridTextColumn Binding="{Binding MontantTVA, StringFormat='{}{0:#.##,0}'}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="TVA"/>
<DataGridTextColumn Binding="{Binding MontantTTC, StringFormat='{}{0:#.##,0}'}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="TTC"/>
<DataGridTextColumn Binding="{Binding Amortfin, StringFormat='{}{0:#.##,0}'}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="Amortissement"/>
<DataGridTextColumn Binding="{Binding Encours, StringFormat='{}{0:#.##,0}'}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="Encours financier"/>
<DataGridCheckBoxColumn Binding="{Binding Fixe, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ElementStyle="{StaticResource DataGridCellRightAlignment}" Header="Figé ?"/>
</DataGrid.Columns>
</DataGrid>
最后是 View :
//Constructeur de la fenêtre
public simulationform()
{
InitializeComponent();
VMSimulation vms = new VMSimulation(); //Instanciation du ViewModel Associé
this.DataContext = vms;
vms.ClosingRequest += (sender, e) => this.Close();
}
datagrid 不刷新 ObservableCollection 并且“Loyer”属性不刷新。我对此进行了调试,发现“命令”工作正常,并且列表包含正确的数据,但未刷新。
当我单击任何列标题时,datagrid 中的数据将正确刷新。奇怪的行为 !!!
提前致谢
最佳答案
通过将值设置为字段,您可以绕过属性 setter 以及 INotifyPropertyChange 触发器,因此在 ctor 中将值设置为属性而不是字段。
如果您的 VM 的构造函数是 Codewise:
#region Constructeur
public VMSimulation()
{
_simulation = new simulation();
Echeancier = new ObservableCollection<Echeance>(_simulation.echeancier); // instead of _echeancier = ...
// LogIt();
}
#endregion
关于WPF 和 MVVM : How to Refresh controls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22861684/