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的绑定

posted @ 2020-05-13 21:47  Lulus  阅读(5367)  评论(0编辑  收藏  举报