我正在开发一个WPF-MVVM应用程序。
我有一个空白的dataGrid,可以在其中添加行。最后一栏显示价格。
我想显示价格总额,作为我加行的度量。
我的代码不起作用。有什么问题吗?
视图
<DataGrid x:Name="dataGridInvoice" ItemsSource="{Binding Collection}"
AutoGenerateColumns="False"
SelectedItem="{Binding Selected, Mode=TwoWay}"
<DataGridComboBoxColumn Header="Ref Supplier"
ItemsSource="{Binding DataContext.Reference, Source={StaticResource ProxyElement}}"
DisplayMemberPath="refsup"
SelectedValueBinding="{Binding refSup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="refsup"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Price/MOQ" Binding="{Binding unitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice, Mode=TwoWay, StringFormat=N2, UpdateSourceTrigger=PropertyChanged}" Width="*" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>ViewModel
public class InvoiceViewModel : ViewModelBase
{
public Context ctx = new Context();
Invoice invoice;
public InvoiceViewModel()
{
Collection = new ObservableCollection<PreInvoice>();
}
private ObservableCollection<PreInvoice> collection;
public ObservableCollection<PreInvoice> Collection
{
get
{
return collection;
}
set
{
collection = value;
OnPropertyChanged("Collection");
Total = Convert.ToString(Collection.Sum(t => t.totalPrice));
}
}
private string _total;
public string Total
{
get
{
return _total;
}
set
{
_total = value;
OnPropertyChanged("Total");
}
}
private void Save()
{
}
private void Delete()
{
}
#region "Command"
private ICommand saveCommand;
private ICommand removeCommand;
#endregionMy模型:
# region wrapper
public class PreInvoice : ViewModelBase, IDataErrorInfo
{
private string _refSup;
public string refSup
{
get
{
return _refSup;
}
set
{
_refSup = value;
OnPropertyChanged("refSup");
}
}
private decimal _quantity;
public decimal quantity
{
get
{
return _quantity;
}
set
{
_quantity = value;
OnPropertyChanged("quantity");
totalPrice = _quantity * _unitPrice;
}
}
private decimal _unitPrice;
public decimal unitPrice
{
get
{
return _unitPrice;
}
set
{
_unitPrice = value;
OnPropertyChanged("unitPrice");
totalPrice = _quantity * _unitPrice;
}
}
private decimal _totalPrice;
public decimal totalPrice
{
get
{
return _totalPrice;
}
set
{
_totalPrice = value;
OnPropertyChanged("totalPrice");
}
}
}发布于 2015-11-06 08:49:43
将财产总额定义改为:
private string _total;
public string Total
{
get
{
_total = Convert.ToString(Collection.Sum(t => t.totalPrice));
return _total;
}
}处理集合的CollectionChanged事件:
public InvoiceViewModel()
{
Collection = new ObservableCollection<PreInvoice>();
Collection.CollectionChanged += Collection_CollectionChanged;
}
void Collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged("Total");
}发布于 2015-11-06 08:37:20
在这个预言中,你似乎混淆了陈述顺序:
public ObservableCollection<PreInvoice> Collection
{
get
{
return collection;
}
set
{
collection = value;
Total = Convert.ToString(Collection.Sum(t => t.totalPrice));
/*or if the above code is not working
collection=Collection.Sum(t => t.totalPrice));
*/
OnPropertyChanged("Collection");
}
}OnPropertyChanged("Collection")方法通过发送带有数据的事件PropertyChanged (“Collection”)来更新UI。
https://stackoverflow.com/questions/33561696
复制相似问题