C# 使用删除按钮删除 WPF Datagrid 中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11759163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:07:38  来源:igfitidea点击:

Deleting rows in WPF Datagrid using delete button

c#wpf

提问by user1372448

I have a data grid on my WPF application window, and the data is bound to an observable collection. In the DataGrid, I have set the property CanUserDeleteRows=Trueand I am able to delete the row by pressing the Delete button on the keyboard.

我的 WPF 应用程序窗口上有一个数据网格,并且数据绑定到一个可观察的集合。在 DataGrid 中,我已经设置了该属性,CanUserDeleteRows=True并且可以通过按键盘上的 Delete 按钮来删除该行。

This doesn't look quite intuitive to me. I want to keep an additional column which has delete button on pressing which the row should get deleted. (something like what can be done in ItemTemplate in ASP.NET)

这对我来说看起来不太直观。我想保留一个附加列,该列在按下应删除的行时具有删除按钮。(类似于可以在 ASP.NET 中的 ItemTemplate 中完成的操作)

<DataGrid x:Name="dgrQuestions" AutoGenerateColumns="False" Height="224" HorizontalAlignment="Left" Margin="42,73,0,0" VerticalAlignment="Top" Width="663" ItemsSource="{Binding QueList}" CanUserAddRows="True" CanUserDeleteRows="True">
            <DataGrid.Columns>                
                <DataGridTextColumn Header="Qu" Binding="{Binding Path=Que, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="An" Binding="{Binding Path=Ans, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="Hi" Binding="{Binding Path=Hi, UpdateSourceTrigger=PropertyChanged}"/>

            </DataGrid.Columns>

How to get this functionality of deleting rows by using a buttoninside the datagrid itself

如何通过使用button数据网格本身的内部来获得删除行的功能

回答by SomeWritesReserved

You can add a DataGridTemplateColumnthat contains a button that invokes the Deletecommand. The DataGrid will handle the Delete command and remove the row.

您可以添加DataGridTemplateColumn包含调用Delete命令的按钮的。DataGrid 将处理删除命令并删除行。

<DataGridTemplateColumn Header="Actions" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="Remove Row" Command="Delete"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

回答by Vlad Bezden

You will have to add DataGridTemplateColumn to your grid. Something like

您必须将 DataGridTemplateColumn 添加到您的网格中。就像是

<DataGrid x:Name="dgrQuestions" AutoGenerateColumns="False" Height="224" HorizontalAlignment="Left" Margin="42,73,0,0" VerticalAlignment="Top" Width="663" ItemsSource="{Binding QueList}" CanUserAddRows="True" CanUserDeleteRows="True">
            <DataGrid.Columns>                
           <DataGridTemplateColumn Header="Delete" Width="75">                 
                <DataGridTemplateColumn.CellTemplate>                     
                    <DataTemplate>                         
                        <Button Content="Delete" Tag="{Binding}" Click="OnDelete"/>                     
                    </DataTemplate>                 
                </DataGridTemplateColumn.CellTemplate>             
            </DataGridTemplateColumn>  
                <DataGridTextColumn Header="Qu" Binding="{Binding Path=Que, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="An" Binding="{Binding Path=Ans, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="Hi" Binding="{Binding Path=Hi, UpdateSourceTrigger=PropertyChanged}"/>

            </DataGrid.Columns>

Then bind your button to any ID, or item {Binding} and you can handle event in code behind (OnDelete) or you can bind button directly to the command, but then you will need to bind SelecteItem to the ViewModel and deal with it on Command executed:

然后将您的按钮绑定到任何 ID 或项目 {Binding},您可以在后面的代码 (OnDelete) 中处理事件,或者您可以将按钮直接绑定到命令,但是您需要将 SelecteItem 绑定到 ViewModel 并处理它执行的命令:

SelectedItem="{Binding SelectedItem, Mode=TwoWay}"