WPF DataGrid 添加Button

<DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="操作" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <Button Content="删除" Tag="{Binding 需要的参数}" Click="Delete_Click" />
                    StackPanel>
                DataTemplate>
            DataGridTemplateColumn.CellTemplate>
        DataGridTemplateColumn>
    DataGrid.Columns>
DataGrid>
private void Delete_Click(object sender, RoutedEventArgs e)
{
    var num = (sender as Button).Tag.ToString();
    //从数据集中删除目标数据
}

示例代码

DataGridBindMultiData.xaml
DataGridBindMultiData.xaml.cs

参考资料

WPF DataGridTemplateColumn添加按钮
WPF中DataGrid控件内Button的Command和CommandParameter的绑定

你可能感兴趣的:(.NET,WPF)