gpt4 book ai didi

c# - wpf 数据网格行中的小计单元格

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:36 24 4
gpt4 key购买 nike

如果触发 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 中有一个复选框,我将其标记为主键,例如 Tag="{Binding tkey}" 我用数据抓取它,但我不知道如何抓取它来自 DataGridCellEditEndingEventArgs 或来自事件的 sender。有没有办法使用标签捕获我来自哪一行?

最佳答案

对不起,如果我误解了。在编辑任何其他有值(value)的单元格中的值后,您需要立即更新 Total,对吗? DataGridCellEditEndingEventArgs 存储您编辑的行数。使用此功能,您可以仅为当前行计算和更新总计值。我在下面创建了一个简单的示例。它对属性 val1-val3 中的值求和,并将总和放在名为“total”的属性中。

这是一个根据数据源结构保存属性的简单类。请注意,您必须实现 INotifyPropertyChanged 接口(interface)以保持 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;
}

最后,DataGrid 的 XAML:

<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>

关于c# - wpf 数据网格行中的小计单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com