WPF在DataGrid添加button在button中添加绑定事件获取行数据
时间: 2024-03-10 21:43:12 浏览: 34
要在DataGrid中添加一个Button,并在Button中绑定事件以获取行数据,可以按照以下步骤操作:
1. 在DataGrid中添加一个列来放置Button。
```
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Click" Click="Button_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
```
2. 在Button的Click事件处理程序中获取所选行的数据。
```
private void Button_Click(object sender, RoutedEventArgs e)
{
// 获取Button所在的DataGridRow
var row = (DataGridRow)DataGrid.ItemContainerGenerator.ContainerFromItem(((FrameworkElement)sender).DataContext);
// 获取行数据
var data = (YourDataClass)row.DataContext;
// 处理数据
// ...
}
```
其中,`YourDataClass`是你在DataGrid中使用的数据类。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)