我剛剛嘗試從MVVM開始,經過多年的使用代碼。當百分比完成更改時,我試圖讓ThisClaimValue更新。 ThisClaimValue旨在顯示基於PercentageComplete的ContractAmt的百分比。無論是人們投入價值,還是離開細胞。我試圖用零代碼來做到這一點,所以沒有內置事件。MVVM中的Datagrid綁定和編輯
我使用EF Database First作爲Description,ContractAmt和BillCurrentAmt。 PercentageComplete和ThisClaimValue作爲EF創建的類的部分類位於單獨的解決方案中。
查看:
<DataGrid Margin="10,10,10,0" RowDetailsVisibilityMode="VisibleWhenSelected" EnableRowVirtualization="True" AutoGenerateColumns="False" ItemsSource="{Binding JCCISelectedList, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding JCCI}" Grid.Row="3">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding ContractAmt}" Header="Value" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding PercentageComplete, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="% Complete" Width="Auto"/>
<DataGridTextColumn Binding="{Binding BillCurrentAmt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="$ Complete" Width="Auto"/>
<DataGridTextColumn Binding="{Binding ThisClaimValue}" Header="This Claim Value" Width="Auto" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
視圖模型:
public class JBProgressBillItemsViewModel : INotifyPropertyChanged, IPageViewModel
{
public AcceptCommand AcceptEvent { get; set; }
public BackCommand BackEvent { get; set; }
public string Name => "JBProgressBillItems";
public JBProgressBillItemsViewModel()
{
HQCOList = Facade.GetVistaHQCO();
AcceptEvent = new AcceptCommand(this);
BackEvent = new BackCommand(this);
}
private bHQCO _hqco;
private bJCCM _jccm;
private bJCCI _jcciSection;
private bJCCI _jcci;
public ObservableCollection<bHQCO> HQCOList { get; }
public ObservableCollection<bJCCM> JCCMList { get; private set; }
public ObservableCollection<bJCCI> JCCISectionList { get; private set; }
public ObservableCollection<bJCCI> JCCIList { get; private set; }
public ObservableCollection<bJCCI> JCCISelectedList { get; private set; }
public bHQCO HQCO
{
get { return _hqco; }
set
{
_hqco = value;
JCCMList = Facade.GetVistaActiveProjects(_hqco.HQCo);
RaisePropertyChanged(nameof(HQCO));
RaisePropertyChanged(nameof(JCCMList));
}
}
public bJCCM JCCM
{
get { return _jccm; }
set
{
_jccm = value;
JCCIList = Facade.GetVistaContractItems(_hqco.HQCo, _jccm.Contract);
JCCISectionList =
new ObservableCollection<bJCCI>(JCCIList.Where(x => x.SICode == "H" || x.SICode == "SH"));
RaisePropertyChanged(nameof(JCCM));
RaisePropertyChanged(nameof(JCCISectionList));
}
}
public bJCCI JCCISection
{
get { return _jcciSection; }
set
{
_jcciSection = value;
try
{
JCCISelectedList = new ObservableCollection<bJCCI>(JCCIList.Where(x => _jcciSection.BillGroup == x.BillGroup && string.IsNullOrWhiteSpace(x.SICode)));
}
catch (ArgumentNullException)
{
}
RaisePropertyChanged(nameof(JCCISection));
RaisePropertyChanged(nameof(JCCISelectedList));
}
}
public bJCCI JCCI
{
get { return _jcci; }
set
{
_jcci = value;
_jcci.ThisClaimValue = value.PercentageComplete * value.ContractAmt/100;
RaisePropertyChanged(nameof(JCCI));
RaisePropertyChanged(nameof(JCCISelectedList));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Error => null;
}
編輯:添加完整的視圖模型上面的代碼。
是什麼bJCCI JCCI?請詳細說明。我的意思是這是一個模型對象? –
如果您會回覆並澄清您的陳述,那麼只有我們可以幫助您。 –
bJCCI是EF中的表格。 – Phalanx