【发布时间】:2017-01-18 02:18:47
【问题描述】:
我有一个 WPF 应用程序。像这样:
<DataGrid x:Name="MetroDataGrid"
CanUserReorderColumns="False"
Grid.Row="1"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
Grid.Column="1"
CanUserResizeRows="False"
CanUserDeleteRows="True"
Margin="1"
AutoGenerateColumns="False"
HeadersVisibility="All"
ItemsSource="{Binding Path=Invoicelines, Mode=TwoWay}"
CanUserSortColumns="False"
SelectionUnit="FullRow">
Invoicelines 是我的 viewmodel 类中的 ObservableCollection 属性。
此网格包含三列:其中两列必须汇总,第三列应包含结果。
请注意,我的 ViewModelClass 现在实现了 INotifyPropertyChanged 接口。
我尝试了 CellEditEnding 事件,但对于数据网格的特定行,我仍然无法获得从前两个单元格更新的第三个单元格。
你如何解决这个问题?
编辑(部分工作......现在的问题是因为我已经为所有列附加了 Cantidad 属性,然后在更改此列中的单元格时的所有行,将此更改传递给其余行同一列... 我还缺少什么?)
XAML
<DataGridTemplateColumn Header="Cant."
Width="100"
MinWidth="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Controls:NumericUpDown Value="{Binding DataContext.Cantidad, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
Minimum="0"
Interval="0.5"
StringFormat="0.000"
HideUpDownButtons="True"
></Controls:NumericUpDown>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
视图模型
class InvoiceViewModel : ViewModelBase
{
private ObservableCollection<InvoiceLine> invoicelines;
public ObservableCollection<InvoiceLine> Invoicelines{
set {
invoicelines = value;
OnPropertyChanged("Invoicelines");
}
get { return invoicelines; }
}
public decimal Cantidad {
get { return cantidad; }
set {
if (Equals(value, cantidad)) return;
cantidad = value;
OnPropertyChanged();
}
}
public decimal Preciounit {
get { return preciounit; }
set
{
if (Equals(value, preciounit)) return;
preciounit = value;
OnPropertyChanged();
}
}
}
ViewModelBase
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
型号
public class InvoiceLine
{
public decimal Cantidad { get; set; }
public decimal Preciounit { get; set; }
public decimal Subtotal { get; set; }
}
【问题讨论】:
标签: c# wpf data-binding datagrid