我试图在CellEditEnding
触发时动态地对几行进行小计。现在,我循环遍历整个集合以获得总数并将其放入总单元格中。请参阅以下代码:
private void CalculateTotals()
{
foreach (var i in reviewItems) // total pay is calculated on data load
{
if (i.Total_Pay == null || i.IsApproved != true) //total pay is recalculated on save
{
i.Total_Pay = GetValue(i.Total_Bonus_Items) + GetValue(i.Total_Hourly_Pay) +
GetValue(i.Total_Line_Items) + GetValue(i.Total_Sick) +
GetValue(i.Total_Adjustments) + GetValue(i.Total_Vacation);
SavedSummary = false;
}
}
}
XAML遵循
<DataGrid x:Name="SummaryGrid" Margin="10,66,10,0" CellEditEnding="SummaryGrid_CellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Employee" Binding="{Binding Emp_Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Period End" Binding="{Binding Week_End_Date, StringFormat=MM/dd/yyyy}" IsReadOnly="True" />
<DataGridTextColumn Header="Load Pay" Binding="{Binding Total_Line_Items}" Visibility="{Binding Source={x:Reference LoadVisibility}}" IsReadOnly="True"/>
<DataGridTextColumn Header="Extra Items" Binding="{Binding Total_Bonus_Items}" Visibility="{Binding Source={x:Reference LoadVisibility}}" IsReadOnly="True"/>
<DataGridTextColumn Header="Hours" Binding="{Binding Total_Hourly_Pay}" Visibility="{Binding Source={x:Reference HourlyVisibility}, Path=Visibility}" IsReadOnly="True"/>
<DataGridTextColumn Header="Vacation" Binding="{Binding Total_Vacation}" Visibility="{Binding Source={x:Reference HourlyVisibility}, Path=Visibility}"/>
<DataGridTextColumn Header="Sick" Binding="{Binding Total_Sick}" Visibility="{Binding Source={x:Reference HourlyVisibility}, Path=Visibility}"/>
<DataGridTextColumn Header="Adjustments" Binding="{Binding Total_Adjustments}" Visibility="{Binding Source={x:Reference HourlyVisibility}, Path=Visibility}"/>
<DataGridTextColumn Header="Total Pay" Binding="{Binding Total_Pay}"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
Approved
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsApproved, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我在MainWindow中有一个Checkbox,我标记为主键,例如Tag="{Binding tkey}"
,我抓住了数据,但我无法弄清楚如何从DataGridCellEditEndingEventArgs
或从来自活动的sender
。有没有办法捕获我使用标签的哪一行?
答案 0 :(得分:0)
对不起,如果我误解了。在任何其他有价值的单元格中编辑值后,您需要立即更新Total
,对吧? DataGridCellEditEndingEventArgs
存储您编辑的行数。使用此功能,您只能计算和更新当前行的总值。
我在下面创建了一个简单的例子。它将属性val1-val3中的值相加,并将总和放在名为“total”的属性中。
这是一个根据数据源结构保存属性的简单类。请注意,您必须实现INotifyPropertyChanged
接口以保持UI更新。
public class DataItem:INotifyPropertyChanged
{
public DataItem(int v1,int v2,int v3)
{
val1 = v1;
val2 = v2;
val3 = v3;
total = v1 + v2 + v3;
}
private int _val1;
public int val1
{
get { return _val1; }
set { _val1 = value; InvokePropertyChanged(new PropertyChangedEventArgs("val1"));}
}
private int _val2;
public int val2
{
get { return _val2; }
set { _val2 = value; InvokePropertyChanged(new PropertyChangedEventArgs("val2")); }
}
private int _val3;
public int val3
{
get { return _val3; }
set { _val3 = value; InvokePropertyChanged(new PropertyChangedEventArgs("val3")); }
}
private int _total;
public int total
{
get { return _total; }
set { _total = value; InvokePropertyChanged(new PropertyChangedEventArgs("total")); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
这是放置DataGrid的MainWindow的代码隐藏,这里数据被填充并加载到网格中,同时处理datagrid_CellEditEnding
事件。
public MainWindow()
{
InitializeComponent();
list = new ObservableCollection<DataItem>();
DataItem i1 = new DataItem(3,6,8);
DataItem i2 = new DataItem(1, 2, 6);
DataItem i3 = new DataItem(9, 7, 22);
list.Add(i1);
list.Add(i2);
list.Add(i3);
datagrid.ItemsSource = list;
}
public ObservableCollection<DataItem> list;
private void datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
int index = e.Row.GetIndex();
list[index].total = list[index].val1 + list[index].val2 + list[index].val3;
}
最后,XAML为DataGrid
:
<DataGrid x:Name="datagrid" AutoGenerateColumns="False" CellEditEnding="datagrid_CellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Val1" Binding="{Binding Path=val1, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Val2" Binding="{Binding Path=val2, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Val3" Binding="{Binding Path=val3, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="total" FontWeight="Black" Binding="{Binding Path=total}" />
</DataGrid.Columns>
</DataGrid>