WPF 编辑 Datagrid 单元格值然后保存,编辑时我正在加载 "application in break mode"

WPF Edit Datagrid cell value and then save, While editing I am lading into "application in break mode"

我是 WPF 新手。请帮我。 有 3 个文本框,即 itemNO、qty 和 discount,还有一个添加按钮。在这些文本框中填写值然后单击添加按钮后,我希望将项目添加到 DataGrid 中。直到在 DataGrid 中添加 Item 一切都很好。但是在尝试编辑 Datagrid 单元格的项目时,我进入了中断模式。我为此编写的代码是

public class OrderItem
{
    public string ItemID { get; set; }
    public string ItemName { get; set; }
    public string Quantity { get; set; }
    public string Discount { get; set; }
    public int Price { get; set; }
}
private OrderItem GetOrder(string itemID, string qty, string discount)
{
    OrderItem orderItem = null;

    {
        DataView dv = BS.GetDataFromDb("where Id = " + itemID);
        if(dv != null && dv.Count > 0)
        {
            orderItem = new OrderItem();
            orderItem.ItemID = itemID;
            orderItem.ItemName = dv[0]["Name"].ToString();
            orderItem.Price = int.Parse(dv[0]["Price"].ToString());
            orderItem.Quantity = qty;
            orderItem.Discount = discount;
        }
        if (dv == null || dv.Count == 0)
            MessageBox.Show("Wrong item no: " + itemID, "Unavailable Item No", MessageBoxButton.OK, MessageBoxImage.Error);
    }
    return orderItem;
}
private void btnAddOrder_Click(object sender, RoutedEventArgs e)
{
   try {
       string discount = (txtDiscount.Text.Trim() == string.Empty ? "0" : txtDiscount.Text.Trim());
       int qty = int.Parse(txtQty.Text.Trim() == string.Empty ? "0" : txtQty.Text.Trim());
       int itemNo = int.Parse(txtItemNo.Text.Trim() == string.Empty ? "0" : txtItemNo.Text.Trim());
       if (qty <=0) { 
            MessageBox.Show("Please insert valid quantity", "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }
        if (itemNo <= 0) {
            MessageBox.Show("Please insert valid item number", "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }

        OrderItem o = GetOrder(txtItemNo.Text, txtQty.Text, discount);
        if(o != null)
        { 
            grdOrderList.Items.Add(new OrderItem { ItemName = o.ItemName, Price = o.Price, Quantity = o.Quantity, Discount = o.Discount });
            if (orderItems == null)
                orderItems = new List<OrderItem>();
            orderItems.Add(o);
            txtItemNo.Text = txtQty.Text = txtDiscount.Text = string.Empty;
        }
    }
    catch (Exception ex) {
        MessageBox.Show("Error 69: "+ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

直到通过单击“添加项目”按钮添加项目 ID、数量和折扣,一切正常。在 DataGrid 中,为了编辑数量列的单元格值,我将该列设置为可编辑(即 ReadOnly = "False")。 但是在尝试编辑 Quantity 的单元格值时,我进入了中断模式(即应用程序处于中断模式)。我将非常感谢您的帮助。下面是 XAML 代码

<StackPanel Name="YourOrder">
      <StackPanel>
          <TextBox Name="txtItemNo" />
          <TextBox Name="txtQty"  />
          <TextBox Name="txtDiscount"  />                           
          <Button Name="btnAddOrder" Click="btnAddOrder_Click" Content="Add to order" ToolTip="Adds item to order" />
      </StackPanel>
      <DataGrid Name="grdOrderList">
          <DataGrid.Columns>
               <DataGridTextColumn Header="Item Name" Binding="{Binding ItemName}" MinWidth="100" IsReadOnly="True" />
               <DataGridTextColumn x:Name="hdrPrice" Header="Price" Binding="{Binding Price}" MinWidth="20" IsReadOnly="True"  CellStyle="{StaticResource DgcRight}" />
               <DataGridTextColumn x:Name="hdrQty" Header="Qty" IsReadOnly="False" MinWidth="20" Binding="{Binding Quantity}" />
               <DataGridTextColumn x:Name="hdrDiscount"  Header="Discount" Binding="{Binding Discount}" MinWidth="50" CellStyle="{StaticResource DgcRight}" />
          </DataGrid.Columns>
     </DataGrid>
</StackPanel>

在您的 .xaml 中,尝试添加以下属性:

DataGrid Name="grdOrderList" CanUserAddRows="True"

我还建议,与其尝试直接在视图中更新列表项,不如使用绑定到作为视图数据源的 ViewModel 中的 ObservableCollection。这样,您只需在代码中更新集合,并且您的列表可以通过不直接在链接到视图的代码隐藏(.xaml.cs 文件)中的操作进行更新。为此,您的 DataGrid 定义还将添加:

DataGrid Name="gdrOrderList" CanUserAddRows="True" ItemsSource={"Binding ListInViewModel"}