Below is my Xaml and ViewModel class looks like. I try to change the existing row or add new row but both of them doesnt work. When I click empty row, it looses the focus. how I can achieve it directly on Xaml or in ViewModel without using code behind?
<UserControl x:Class="ucCustomer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:myWPF="clr-namespace:myWPF" xmlns:vm="clr-namespace:myWPF.ViewModels" mc:Ignorable="d" MinWidth="750" MinHeight="650" ><Grid><Grid.DataContext><vm:CustomerVM /></Grid.DataContext><Button x:Name="btnSave" Margin="0,10,10,0" Width="120" Height="25" Content="Save Changes" Command="{Binding btnClick}" HorizontalAlignment="Right" VerticalAlignment="Top"/><DataGrid x:Name="grdData" AutoGenerateColumns="False" Margin="10,50,10,5" Style="{StaticResource myDataGrid}" ItemsSource="{Binding Customers, Mode=TwoWay}"><DataGrid.Columns><DataGridTextColumn Header="CustomerId" Binding="{Binding Path=CustomerId}" Width="50"></DataGridTextColumn><DataGridTextColumn Header="CustomerName" Binding="{Binding Path=CustomerName}" Width="150"></DataGridTextColumn><DataGridTextColumn Header="AccountId" Binding="{Binding Path=AccountId}" Width="130"></DataGridTextColumn><DataGridTextColumn Header="AccountName" Binding="{Binding Path=AccountName}" Width="200"></DataGridTextColumn><DataGridTextColumn Header="ModifyDate" IsReadOnly="True" Binding="{Binding Path=ModifyDate}" Width="130"></DataGridTextColumn></DataGrid.Columns></DataGrid></Grid>
Namespace ViewModels Public Class CustomerVM Inherits BaseViewModel Implements ICustomerVM Private _Customer s As New ObservableCollection(Of Models.Customer) Private mySaveCommand As ICommand Private myLoadCommand As ICommand Public Sub New() mySaveCommand = New Commands.SaveCustomers(Me) myLoadCommand = New Commands.LoadCustomer() _Customers = GetCustomers() End Sub Public Property Customers() As ObservableCollection(Of Models.Customer) Get Return _Customers End Get Set(value As ObservableCollection(Of Models.Customer)) _Customers = value End Set End Property Public ReadOnly Property btnClick() As ICommand Get Return mySaveCommand End Get End Property Function GetCustomers() As ObservableCollection(Of Models.Customer) Implements ICustomerVM.GetCustomer If _Customers Is Nothing OrElse _Customers.Count = 0 Then myLoadCommand.Execute(_Customers) Return _Customers End Function Public Function SaveCustomerstoDB() As Integer Dim myContext As New Models.myModelContext Return myContext.SaveChanges() End Function End Class End Namespace Namespace ViewModels Public Class BaseViewModel Implements INotifyPropertyChanged Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged Protected Sub OnPropertyChanged(propertyName As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub Protected Sub SetAndNotify(Of T)(ByRef field As T, value As T, propertyName As String) If Not EqualityComparer(Of T).[Default].Equals(field, value) Then field = value OnPropertyChanged(propertyName) End If End Sub End Class End Namespace
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."