I am using Galasoft MVVM Light. I have a DataGrid with four columns with a gross amount, a VAT-rate, VAT-amount and net amount. When the user changes one of the values then I need to recalculate the other three. I have a relay command attached to the CellEditEnding event of the DataGrid to activate code in the ViewModel that does the calculation. But I need to know which value has been changed in order to do the calculation correctly. How do I specify in XAML which column/cell has triggered the event, ie how to bind the command parameter to the name of the triggering column/cell?
<DataGrid
....>
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<mvvm:EventToCommand Command="{Binding CalculateAusgabe_Command,
Mode=OneWay,
UpdateSourceTrigger=PropertyChanged,
diag:PresentationTraceSources.TraceLevel=High}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridTextColumn}}, Path=Name}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
In ViewModel the code is:
public RelayCommand<string> CalculateAusgabe_Command { get; set; }
...
CalculateAusgabe_Command = new RelayCommand<string>(CalculateAusgabe);
and finally the procedure that does the calculation:
void CalculateAusgabe(string colName)
{
if (currBetrag.USt == null) return;
switch (colName)
{
case "colBetBtto":
case "colBetUStS":
if (currBetrag.BttoBetrag != 0M)
{
currBetrag.UStBetrag = Math.Round((currBetrag.BttoBetrag / (100M + currBetrag.USt.UStProz)) * currBetrag.USt.UStProz, 2);
currBetrag.NttoBetrag = currBetrag.BttoBetrag - currBetrag.UStBetrag;
}
break;
case "colBetUStB":
...
The current row of the DataGrid is bound to currBetrag
object. Everything works fine, CalculateAusgabe
is activated when the event is triggered but no matter what I do the parameter colName always is null. Any help on this problem is appreciated.
You could perform the calculations in the setters of the currBetrag class, eg:
public class currBetrag : INotifyPropertyChanged
{
private decimal _bttoBetrag;
public decimal BttoBetrag
{
get { return _bttoBetrag; }
set
{
_bttoBetrag = value;
OnPropertyChanged();
CalculateAusgabe("BttoBetrag");
}
}
private decimal _uStBetrag;
public decimal UStBetrag
{
get { return _uStBetrag; }
set
{
_uStBetrag = value;
OnPropertyChanged();
CalculateAusgabe("UStBetrag");
}
}
private void CalculateAusgabe(string colName)
{
if (USt == null) return;
switch (colName)
{
//set the value of all fields...
_uStBetrag = ?;
}
//and raise the PropertyChanged event for all involved properties
OnPropertyChanged("UStBetrag");
OnPropertyChanged("NttoBetrag");
//...
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Note that is considered a bad practice not to use English member names regardless of what your native language is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.