[英]Calculated column in a dataGrid bound with a ObservableCollection
我正在構建WPF-MVVM應用程序。 我有一個與ObservableCollection綁定的dataGrid。
在此集合中,我有一列“數量”,一列“價格/最小起訂量”和一列“合計”,在邏輯上必須等於“數量*價格=合計”。
因此,每次我添加一行並填充“數量和價格”時,都必須計算“總計”列。
我怎樣才能做到這一點?
<DataGrid x:Name="dataGridInvoice" Margin="5" Grid.Row="1"
ItemsSource="{Binding Collection}"
AutoGenerateColumns="False"
SelectedItem="{Binding Selected, Mode=TwoWay}"
SelectionMode="Extended" SelectionUnit="FullRow" AddingNewItem="dataGridInvoice_AddingNewItem">
<DataGrid.Columns>
<DataGridTextColumn Header="SuppNb" Binding="{Binding suppInvNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Shop" Binding="{Binding shop, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Date" Binding="{Binding date, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Supplier" Binding="{Binding supplier, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridComboBoxColumn Header="Ref Supplier"
ItemsSource="{Binding Products, Mode=OneWay, Source={StaticResource supplier}}"
DisplayMemberPath="refsup"
SelectedValueBinding="{Binding refSupp}"
SelectedValuePath="refsup"
Width="*"/>
<DataGridTextColumn Header="Description" Binding="{Binding description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridComboBoxColumn Header="Unit"
ItemsSource="{Binding Collection, Mode=OneWay, Source={StaticResource unit}}"
DisplayMemberPath="unit1"
SelectedValueBinding="{Binding unit}"
SelectedValuePath="idunit"
Width="*"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Prix/MOQ" Binding="{Binding unitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
public class InvoiceViewModel : ViewModelBase
{
public Context ctx = new Context();
public InvoiceViewModel()
{
Get(false);
}
private ObservableCollection<Invoice> collection;
public ObservableCollection<Invoice> Collection
{
get
{
return collection;
}
set
{
collection = value;
OnPropertyChanged("Collection");
}
}
private Invoice _selected;
public Invoice Selected
{
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("Selected");
}
}
private void Get(bool loadDataFirst)
{
if(loadDataFirst)
ctx.Invoices.Load();
Collection = ctx.Invoices.Local;
}
private void Save()
{
ctx.SaveChanges();
}
private void Delete()
{
var id = Selected;
var invoice = (from i in ctx.Invoices
where i.idInvoice == id.idInvoice
select i).SingleOrDefault();
Collection.Remove(invoice);
}
private Invoice _currentItem;
public Invoice CurrentItem
{
get
{
return _currentItem;
}
set
{
_currentItem = value;
OnPropertyChanged("CurrentItem");
}
}
#region "Command"
private ICommand saveCommand;
private ICommand removeCommand;
public ICommand SaveCommand
{
get
{
return saveCommand ?? (saveCommand = new RelayCommand(p => this.Save(), p => this.CanSave()));
}
}
private bool CanSave()
{
return true;
}
#endregion
}
public partial class Invoice
{
public int idInvoice { get; set; }
public string invNumber { get; set; }
public string suppInvNumber { get; set; }
public Nullable<int> supplier { get; set; }
public Nullable<int> shop { get; set; }
public Nullable<System.DateTime> date { get; set; }
public string refSupp { get; set; }
public string description { get; set; }
public string unit { get; set; }
public Nullable<decimal> quantity { get; set; }
public Nullable<decimal> unitPrice { get; set; }
public Nullable<decimal> totalPrice { get; set; }
public virtual foodSupplier foodSupplier { get; set; }
public virtual shop shop1 { get; set; }
}
坎蒂努,
我看到兩種方式:
更改發票類,使其引發事件
公共部分類發票:ViewModelBase {// .... private可空數量;
public Nullable<decimal> Quantity { get { return quantity; } set { quantity = value; base.OnPropertyChanged(); if (quantity.HasValue && unitPrice.HasValue) TotalPrice = quantity * unitPrice; } } private Nullable<decimal> unitPrice; public Nullable<decimal> UnitPrice { get { return unitPrice; } set { unitPrice = value; base.OnPropertyChanged(); if (quantity.HasValue && unitPrice.HasValue) TotalPrice = quantity * unitPrice; } } private Nullable<decimal> totalPrice; public Nullable<decimal> TotalPrice { get { return totalPrice; } set { totalPrice = value; base.OnPropertyChanged(); } }
}
在您的GUI中放入一些代碼以檢測版本結束並生產產品。 我認為這不是計算價格的責任,但是從技術上講,這是可能的。
private void DataGridInvoiceRowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { Invoice invoice = e.Row.DataContext as Invoice; // update Invoice object if needed if (invoice != null) { // .... } } } private void DataGridInvoiceCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { // ... }
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.