簡體   English   中英

更新DataGrid WPF中的單元格

[英]update cell in datagrid wpf

我有一個發票程序,可以跟蹤客戶可能想購買的選定商品。 每次按下“添加項目”按鈕,QTY單元格都會增加。 我遇到的問題是該值正在后台升級,但在單元格本身中未更改。 但是,如果單擊該單元格,該值將更新。 但是,我希望它在我單擊“添加項目”按鈕后立即更新。 單擊此按鈕可以更新單元格信息嗎? 這是我與按鈕按下一起使用的代碼

public partial class MainWindow : Window
}
private void btnAddItem_Click(object sender, RoutedEventArgs e)
    {
        addDataGridRow(invoiceItemID, cbxItems.SelectedItem.ToString(), Convert.ToDouble(txtPrice.Text));
    }

    private void addDataGridRow(int id, string itemName, double itemPrice)
    {
        //lstDataItem.Add(new DataItem { sQTY = count.ToString(), sNAME = itemName, sPRICE = itemPrice, sRemove = "Delete Item" });
       // Rows = new ObservableCollection<DataItem> { new DataItem () };
        for (int i = 0; i < Rows.Count; i++)
        {
            if (Rows[i].sNAME == itemName)
            {
                Rows[i].sQTY = Rows[i].sQTY + 1;
                id = Rows[i].id;
                bSameItem = true;
            }
        }
        if (id == 0)
        {
            Rows.Add(new DataItem());
        }
        else if (!bSameItem)
        {
            Rows.Add(new DataItem());
        }


        if (!bSameItem)
        {
            Rows[id].id = id;
            Rows[id].sQTY = 1;
            Rows[id].sNAME = itemName;
            Rows[id].sPRICE = itemPrice;
            invoiceItemID++;
            dgInvoice.ItemsSource = Rows;

        }
        bSameItem = false;
    }
}

public class DataItem
{
    public int id { get; set; }
    public int sQTY { get; set; }
    public string sNAME { get; set; }
    public double sPRICE { get; set; }
}

我不知道您是否需要我的XAML,但這是為了以防萬一

        <DataGrid x:Name="dgInvoice" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Height="608" Width="488" AutoGenerateColumns="False" ItemsSource="{Binding}" RowHeight="25">
            <DataGrid.Columns>
                <DataGridTextColumn Header="QTY" Binding="{Binding Path=sQTY}" Width="100"/>
                <DataGridTextColumn Header="NAME" Binding="{Binding Path=sNAME}" Width="200"/>
                <DataGridTextColumn Header="PRICE" Binding="{Binding Path=sPRICE}" Width="80"/>
                <DataGridTemplateColumn Width="100">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Button Height="20">Delete Item</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

當我搜索此主題時,對wpf的響應並不多。 任何幫助將不勝感激。

您必須在類DataItem上實現INotifyPropertyChanged接口,以便其屬性中的任何更改都可以反映到UI上。

public class DataItem : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   private int id;
   public int Id
   {
      get { return id; }
      set
      {
         if(id != value)
         {
            id= value;
            OnPropertyChanged("Id");
         }
      }
   }

  // Create the OnPropertyChanged method to raise the event 
  protected void OnPropertyChanged(string name)
  {
     PropertyChangedEventHandler handler = PropertyChanged;
     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(name));
     }
  }
}

使其他屬性遵循與上述Id相同的語法,您將看到屬性值的任何更新都將反映在GUI上。

我只需要做dgInvoices.Items.Refresh();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM