9

如何在 WPF 中为数据网格(包含两个字段)中的每一行添加删除按钮。而 datagrid itemsource 是

ObservableCollection<Result>

 public class Result : INotifyPropertyChanged
{
    public string FriendlyName { get; set; }
    public string Id { get; set; }

    public Result(string name, string id)
    {
        this.FriendlyName = name;
        this.Id = id;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string property)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    #endregion
}

}

4

1 回答 1

27

您可以添加 aDataGridTemplateColumn并将 a 添加Button到其CellTemplate. 然后使用内置ApplicationCommands.Delete或您自己ICommandButton

<DataGrid ItemsSource="{Binding Results}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="FriendlyName"
                            Binding="{Binding FriendlyName}"/>
        <DataGridTextColumn Header="Id"
                            Binding="{Binding Id}"/>
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="Delete"
                            Command="Delete"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

更新
如果您不想使用内置的,Delete您可以使用自己的ICommand. 这是一个简短的示例RelayCommand,可在以下链接中找到:http: //msdn.microsoft.com/en-us/magazine/dd419663.aspx。另外,我在这里上传了它:RelayCommand.cs

<DataGrid ItemsSource="{Binding Results}"
          SelectedItem="{Binding SelectedResult}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <!-- ... -->
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="Delete"
                            Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                              Path=DataContext.DeleteCommand}"
                            CommandParameter="{Binding}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

在您的视图模型或代码中

private Result m_selectedResult;
public Result SelectedResult
{
    get { return m_selectedResult;}
    set
    {
        m_selectedResult = value;                
        OnPropertyChanged("SelectedResult");
    }
}

private bool CanDelete
{
    get { return SelectedResult != null; }
}

private ICommand m_deleteCommand;
public ICommand DeleteCommand
{
    get
    {
        if (m_deleteCommand == null)
        {
            m_deleteCommand = new RelayCommand(param => Delete((Result)param), param => CanDelete);
        }
        return m_deleteCommand;
    }
}

private void Delete(Result result)
{
    Results.Remove(result);
}
于 2012-06-05T06:29:32.437 回答