.net 为数据网格中的每一行添加删除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10892233/
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-09-03 16:27:41  来源:igfitidea点击:

Add delete button for every row in datagrid

.netwpfdatagrid

提问by RATHI

how to add a delete button for every row in a data grid (contains two fields) in WPF. while datagrid itemsource is

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

ObservableCollection<Result>

and

 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
}

}

}

回答by Fredrik Hedblad

You can add a DataGridTemplateColumnand add a Buttonto its CellTemplate. Then use the built in ApplicationCommands.Deleteor your own ICommandfor the Button

您可以添加 a 并将 aDataGridTemplateColumn添加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>

Update
If you don't want to use the built in Deleteyou can use your own ICommand. Here is a short example with RelayCommandwhich can be found in the following link: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx. Also, I uploaded it here: RelayCommand.cs

更新
如果您不想使用内置的,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>

And in your viewmodel or code behind

而在你的视图模型或后面的代码中

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);
}