WPF的DataGrid - 巴顿在列,获得该行从它来到Click事件处理程序 [英] WPF DataGrid - Button in a column, getting the row from which it came on the Click event handler
问题描述
我设置我的WPF DataGrid的的ItemSource到对象的列表从我DAL返回。我还添加其中包含一个按钮,一个额外的列时,XAML如下。
I've set the itemsource of my WPF Datagrid to a List of Objects returned from my DAL. I've also added an extra column which contains a button, the xaml is below.
<toolkit:DataGridTemplateColumn MinWidth="100" Header="View">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Button_Click">View Details</Button>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
这使得罚款。不过在 Button_Click 方式,有什么方法可以让我得到那里的按钮所在的数据网格的行?更具体地讲,我对象的属性之一是ID,我希望能够将此传递到事件处理程序的另一种形式的构造函数。
This renders fine. However on the Button_Click method, is there any way I can get the row on the datagrid where the button resides? More specifically, one of the properties of my objects is "Id", and I'd like to be able to pass this into the constructor of another form in the event handler.
private void Button_Click(object sender, RoutedEventArgs e)
{
//I need to know which row this button is on so I can retrieve the "id"
}
也许,我需要在我的XAML额外的东西,或者也许我在一个迂回的方式去这件事?任何帮助/咨询AP preciated。
Perhaps I need something extra in my xaml, or maybe I'm going about this in a roundabout way? Any help/advice appreciated.
推荐答案
基本上你的按钮将继承行数据对象的datacontext的。我打电话它作为MyObject来,希望MyObject.ID是你想要的。
Basically your button will inherit the datacontext of a row data object. I am calling it as MyObject and hope MyObject.ID is what you wanted.
private void Button_Click(object sender, RoutedEventArgs e)
{
MyObject obj = ((FrameworkElement)sender).DataContext as MyObject;
//Do whatever you wanted to do with MyObject.ID
}
这篇关于WPF的DataGrid - 巴顿在列,获得该行从它来到Click事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!