【发布时间】:2016-12-16 11:43:47
【问题描述】:
我有一个 DataGrid,它的每一行都包含一个像这样的删除按钮:
<Button Command="{Binding DeleteRowCriterionCommand}">Delete</Button>
但我不知道如何在不使用 DataGrid.Name 的情况下获取 currentItem 属性
<!-- 2ND : CRITERIA -->
<Grid>
<DataGrid ItemsSource="{Binding UserCriteria, Mode=TwoWay}" SelectedItem="{Binding SelectedItemDG, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<!--TEXTBOX FOR SQL VALUES-->
<DataGridTemplateColumn Header="SQLValue" Width="600">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- BUTTON FOR DELETING -->
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding DeleteRowCriterionCommand}">Delete</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
我正在使用 MVVMLight,所以我知道如何将我的命令连接到我的 ViewModel,但唯一缺少的是获取我单击按钮所在行的索引。
这是使用 DataGrid.Name 的函数:
public void DeleteRowCriterion()
{
// I would like to replace the first line by not using DataGridName
// int currentRowIndex = DataGridName.Items.IndexOf(CriteriaDG.CurrentItem);
// UserCriteria.RemoveAt(currentRowIndex);
}
如果有人知道怎么做,我将不胜感激!
【问题讨论】:
-
能否请您包含
<DataGrid的完整xaml,我认为您必须添加一些CommandParameter="" -
我编辑了我的帖子,添加了整个
DataGrid。确实,我没用CommandParameter,不知道这个属性 -
需要添加一些命令参数:try this example
-
我不知道我是否清楚,但我不想将一个类的属性作为参数,我只想获取按钮所在行的索引