【发布时间】:2021-10-30 05:10:55
【问题描述】:
由于我是 C# WPF 的新手,因此我尝试使用试错法。
我想做的是根据一些用户操作更改DataGrid 中一行的外观。
但是,通过阅读互联网上的信息,我了解到“正确”的方法是:
你不应该写一个方法或一个函数:你需要写一个事件。
好的,好的,所以我开始在 XAML 中执行此操作:
<DataGrid x:Name="dg_Areas" ... LoadingRow="View_dg_Areas"/>
显然,View_dg_Areas 事件处理程序应具有以下签名:
private void View_dg_Areas(object sender, DataGridRowEventArgs e) { }
然后呢?
我很天真地开始如下:
private void View_dg_Areas(object sender, DataGridRowEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
System.Diagnostics.Debug.WriteLine(e.Row.ToString());
System.Diagnostics.Debug.WriteLine(e.Row.Item.ToString());
}
我的想法是从中学习如何找到相应行的信息(有没有办法读取某一列的值?),但我没有得到任何地方。
我可以告诉你DataGrid 链接到DataTable,如下:
dg_Areas.ItemsSource = dataSet.Tables["Areas"].DefaultView;
如何将事件参数e 与DataGrid 所代表的DataTable 联系起来?
【问题讨论】:
-
事件不会是
LoadingRow,而是用户触发的任何事件,例如button_Click -
在 DataGrid 事件和大多数控件事件中,发送者是发生事件的 DataGrid。因此,在方法@987654333@ 中,您可以进行类似于:
DataGrid grid = (DataGrid) sender的直接转换。但如果只有一个网格使用该功能,您可以访问网格dg_Areas -
@Cleptus:我有一些GUI编程经验,所以我知道
sender对象。我的问题的背景思想是了解DataGridRowEventArgs。你能举个例子来说明它的用法吗?
标签: c# wpf xaml datatable datagrid