I've just tried to start with MVVM after years of using code behind. I'm trying to get ThisClaimValue to update when PercentageComplete changes. ThisClaimValue is meant to show the percentage of ContractAmt, based on PercentageComplete. Either while people are putting in the value, or when they leave the cell. I'm trying to do this with zero code behind, so no built in events.
I'm using EF Database First for Description, ContractAmt, and BillCurrentAmt. PercentageComplete and ThisClaimValue are located in a seperate solution as a partial class to the class that EF created.
View:
<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>
ViewModel:
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;
}
EDIT: Added full view model code above.