WPF DataGrid 添加编辑和删除按钮

xaml

<DataGrid Name="userList"  AutoGenerateColumns="False" CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Button Content="编 辑"  Tag="{Binding id}" Click="UserEditClick" />
                                    <Button Content="删 除" Tag="{Binding id}" Click="UserDeleteClick" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

从sender中获取id

private void UserEditClick(object sender, RoutedEventArgs e)
{
     Button btn = sender as Button;
     if (btn != null)
     {
         int id = Convert.ToInt32(btn.Tag);
     }
 }

你可能感兴趣的:(WPF,c#)