我希望通过在WPF DataGrid上方(而不是内部)点击一个按钮来删除行,遵循MVVM模式。虽然在行本身内部的删除按钮可以工作,但它们有点丑陋(必须先选择行),并且由于还有添加和编辑按钮紧挨着删除按钮,我认为放置一个删除按钮会更好。
我的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>
你有没有关于按钮的命令绑定的想法?或者,如果对于在行内的删除按钮始终启用,并且单击其中一个将直接删除相应行而无需先选择该行,则我也会感到满意。