小编THW*_*THW的帖子

如何在WPF中将按钮绑定到DataGrid删除命令

我想按照MVVM模式,通过单击DataGrid上方(而非内部)的按钮从WPF DataGrid删除行。尽管行中的删除按钮本身可以工作,但它们有点丑陋(必须先选择该行),并且由于删除按钮旁边还有添加和编辑按钮,我认为删除按钮更适合。我的ViewModel中的相应部分如下所示:

<Button Grid.Row="0" Grid.Column="0" Content="add"/>
<Button Grid.Row="0" Grid.Column="1" Content="edit"/>
<Button Grid.Row="0" Grid.Column="2" Content="delete"/>
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" 
    ItemsSource="{Binding dataTableListItems}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name"/>
        <!--I don't wan't to use these "in-Row" delete buttons-->
        <DataGridTemplateColumn Header="delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Command="Delete" Content="X"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

关于按钮的命令绑定看起来如何的任何想法?或者,如果始终启用行内删除按钮,并且单击一个按钮将删除相应的行而无需先选择该行,那么我也将感到满意。

wpf mvvm commandbinding

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

commandbinding ×1

mvvm ×1

wpf ×1