从 WPF 中的 DataGridView 中删除行

Remove row from DataGridView in WPF

运行出问题了,我有一个datagrid,列如下 一、二、乘

我可以通过点击键盘上的 del KEY 来删除购买行,但是当我尝试编写我自己的删除功能时,它不起作用 我收到的消息是

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll

附加信息:使用 ItemsSource 时操作无效。使用 ItemsControl.ItemsSource 访问和修改元素。

这是我的代码:

private void buttonRemove_Click(object sender, RoutedEventArgs e)
{
    NumberData.Items.Remove(NumberData.SelectedItems); 

}

public class Numbers : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 

    private double? _one;
    private double? _two;
    private double? _multiply;
    public double? One { get { return _one; } set { _one = value; UpdateValue(); } }
    public double? Two { get { return _two; } set { _two = value; UpdateValue(); } }
    public double? Multiply
    {
        get { return _multiply; }
        set
        {
            _multiply = value; UpdateValue();
        }
    }

    public Numbers(double? pOne, double? pTwo)
    {
        _one = pOne;
        _two = pTwo;
        _multiply = GetMultiply(); 
    }

    private void UpdateValue()
    {
        _multiply = GetMultiply();

        OnPropertyChanged("One");
        OnPropertyChanged("Two");
        OnPropertyChanged("Multiply");
    }

    private double? GetMultiply()
    {
        return _one * _two;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

public class Collection :  ObservableCollection<Numbers>  
{
    public ObservableCollection<Numbers> Numbers { get; set; }      

    public Collection()
    {
        Numbers = new ObservableCollection<Numbers>();

        //Numbers.Add(new Numbers(1, 2));
        //Numbers.Add(new Numbers(2, 2)); 
    }

    public void AddNumbers(double pOne, double pTwo)
    {
        Numbers.Add(new Numbers(pOne,pTwo));
    }

    public void AddNumbersRow()
    {
        Numbers.Add(new Numbers(null, null));
    }
}

<Grid>
    <DataGrid x:Name="NumberData" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="False" LostFocus="StockData_LostFocus" >
        <DataGrid.Columns >
            <DataGridTextColumn Header="Number One" Width="100" Binding="{Binding One, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
            <DataGridTextColumn Header="Number Two" Width="100" Binding="{Binding Two, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
            <DataGridTextColumn Header="Total" Width="100" Binding="{Binding Multiply, BindsDirectlyToSource=True, Mode=TwoWay, StringFormat=\{0:C\}, UpdateSourceTrigger=PropertyChanged}" />
        </DataGrid.Columns>
    </DataGrid>
    <Button x:Name="buttonSave" Content="Save" HorizontalAlignment="Left" Margin="411,11,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <Button x:Name="buttonAddRow" Content="Add Row" HorizontalAlignment="Left" Margin="411,49,0,0" VerticalAlignment="Top" Width="75" Click="buttonAddRow_Click"/>
    <Button x:Name="buttonRemove" Content="Remove" HorizontalAlignment="Left" Margin="411,88,0,0" VerticalAlignment="Top" Width="75" Click="buttonRemove_Click"/>
</Grid>

看起来你一切都设置好了,有一个奇怪的地方:

<DataGrid ItemsSource="{Binding}">

不确定为什么 ItemsSource 是 DataContext。 DataContext 应该是一个视图模型 class,其中包含一些 ObservableCollection 属性,然后您将绑定到该视图模型。

但除此之外,因为您说您的代码可以正常工作; 你不应该像那样手动删除项目

您的按钮应该绑定到一个命令,该命令转到视图模型并修改绑定的集合。示例如下:

myObservableCollection.RemoveAt(0);

由于它是 ObservableCollection,修改将传播到 UI。

非常感谢,我从你的回答中得到了灵感,这是我为删除项目所做的工作

if (NumberData.SelectedIndex == -1)
       {
           System.Windows.MessageBox.Show("nothing selected");

       }
       else
       {
           int indexSelected = Convert.ToInt32(NumberData.SelectedIndex);
           mycollection.Numbers.RemoveAt(indexSelected);
       }