I have a WPF application. Like this:
<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">
Where Invoicelines is a ObservableCollection property in my viewmodel class.
This grid contains three columns: two of them must be summarized and the third column should contains the result.
Notice that my ViewModelClass now implements the INotifyPropertyChanged interface.
I tried CellEditEnding event, and still I cannot get, for ths specifid row of the datagrid, the third cell updated from thw two previous cells.
How can you resolve this?
EDIT (working partially.. Now the problem is because I've attached Cantidad properyy for all a column, then all the rows when change the cell in this columns, pass this change to the rest of the rows in the same column... what else am I missing?)
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>
ViewModel
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));
}
}
Model
public class InvoiceLine
{
public decimal Cantidad { get; set; }
public decimal Preciounit { get; set; }
public decimal Subtotal { get; set; }
}
I tried with the first column like this:
<DataTemplate> <Controls:NumericUpDown Value="{Binding Cantidad2}"></Controls:NumericUpDown> </DataTemplate>
. But still when I debug the compiler is not getting into the Cantidad2 property.. am I missing something?
If you are using the NumericUpDown from MahApps you should try to set the UpdateSource property of the binding to PropertyChanged:
<controls:NumericUpDown Value="{Binding Cantidad2, UpdateSourceTrigger=PropertyChanged}" />
Then the setter of the Cantidad2 property should get invoked provided that the data object in your "Invoicelines" collection actually has a property named "Cantidad2" of the appropriate type (which is probably double
depending on the NumericUpDown Control you are using).
Edit: If you want to bind to a property of the InvoiceViewModel class you should use a RelativeSource:
<controls:NumericUpDown Value="{Binding DataContext.Cantidad, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=DataGrid}}" />