Hi, the logic of your code is unclear to me. I have revised your code, deleted all code-behind (except InitializeComponent) and concentrated all logic in a ViewModel. A better approach would be to use IDataError in StudentModel.
<Window x:Class="Experiment.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Experiment"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="1200" Background="blue" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Window.Resources>
<local:ViewModel x:Key="vm"/>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Frame x:Name="Frame1" Content="{Binding Frame1}" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" NavigationUIVisibility="Hidden"/>
<Frame x:Name="Frame2" Content="{Binding Frame2}" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
<Page
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:local="clr-namespace:Experiment" x:Class="Experiment.Page1"
mc:Ignorable="d" Height="400" Width="1600" Title="Page1" >
<Grid Background="#FF5D5B5B">
<local:DGrid x:Name="dGrid" ItemsSource="{Binding Vm_StudentCollectionSub}" RowHeaderWidth="20" HorizontalAlignment="Left" Margin="278,107,0,0" VerticalAlignment="Top" Height="145" Width="517" AutoGenerateColumns="False" SelectionUnit="CellOrRowHeader" SelectionMode="Single">
<local:DGrid.RowValidationRules>
<local:RowValidation ValidationStep="UpdatedValue"/>
<!--Here I want to add step validation-->
<!--< ValidationStep="UpdatedValue"/>-->
</local:DGrid.RowValidationRules>
<local:DGrid.Columns>
<DataGridTextColumn Header="Name" Width="100" Binding="{Binding M_Name}"/>
<DataGridTextColumn Header="Maths" Width="100" Binding="{Binding M_Maths}"/>
<DataGridTextColumn Header="Science" Width="100" Binding="{Binding M_Science}"/>
<DataGridTextColumn Header="English" Width="100" Binding="{Binding M_English}"/>
</local:DGrid.Columns>
</local:DGrid>
<Button Content="Add" Command="{Binding Cmd}" Margin="716,257,805,112"/>
</Grid>
</Page>
<Page x:Class="Experiment.Page2"
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:local="clr-namespace:Experiment"
mc:Ignorable="d" Height="400" Width="1600"
Title="Page2">
<Grid>
<local:DGrid x:Name="dGrid" ItemsSource="{Binding Vm_StudentCollection}" VerticalAlignment="Top" Height="222" HorizontalAlignment="Left" Width="522" AutoGenerateColumns="False" SelectionUnit="CellOrRowHeader" SelectionMode="Single" RowHeight="22" RowHeaderWidth="15">
<local:DGrid.Columns>
<DataGridTextColumn Header="Name" Width="100" Binding="{Binding M_Name}"/>
<DataGridTextColumn Header="Maths" Width="100" Binding="{Binding M_Maths}"/>
<DataGridTextColumn Header="Science" Width="100" Binding="{Binding M_Science}"/>
<DataGridTextColumn Header="English" Width="100" Binding="{Binding M_English}"/>
<DataGridTextColumn Header="Total" Width="100" Binding="{Binding M_Total}"/>
</local:DGrid.Columns>
</local:DGrid>
</Grid>
</Page>
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace Experiment
{
public class ViewModel : RowValidation, INotifyPropertyChanged
{
public ViewModel()
{
StudentCollection.Add(new StudentModel { M_STId = 1, M_Name = "Santhosh", M_Maths = 70, M_Science = 56, M_English = 45 });
StudentCollection.Add(new StudentModel { M_STId = 2, M_Name = "Mathew", M_Maths = 60, M_Science = 65, M_English = 77 });
StudentCollection.Add(new StudentModel { M_STId = 3, M_Name = "Poly", M_Maths = 40, M_Science = 66, M_English = 45 });
StudentCollection.Add(new StudentModel { M_STId = 4, M_Name = "Tony", M_Maths = 20, M_Science = 88, M_English = 49 });
//
cvsStudentCollection.Source = StudentCollection;
cvsStudentCollection.View.CurrentChanged += StudentCollection_CurrentChanged;
//
cvsStudentCollectionSub.Source = StudentCollectionSub;
//
Frame1 = new Page1() { DataContext = this };
Frame2 = new Page2() { DataContext = this };
}
public Page Frame1 { get; set; }
public Page Frame2 { get; set; }
private ObservableCollection<StudentModel> StudentCollection { get; set; } = new ObservableCollection<StudentModel>();
private ObservableCollection<StudentModel> StudentCollectionSub { get; set; } = new ObservableCollection<StudentModel>();
private CollectionViewSource cvsStudentCollection = new CollectionViewSource();
private CollectionViewSource cvsStudentCollectionSub = new CollectionViewSource();
public ICollectionView Vm_StudentCollection { get => cvsStudentCollection.View; }
public ICollectionView Vm_StudentCollectionSub { get => cvsStudentCollectionSub.View; }
private void StudentCollection_CurrentChanged(object sender, System.EventArgs e)
{
if (Vm_StudentCollection.CurrentItem != null)
{
StudentCollectionSub.Clear();
StudentCollectionSub.Add(((StudentModel)Vm_StudentCollection.CurrentItem).Copy());
}
}
public ICommand Cmd { get => new RelayCommand(CmdExec); }
private void CmdExec(object obj)
{
for (int i = StudentCollectionSub.Count-1; i >= 0; i--)
{
var s = StudentCollectionSub[i]; ;
StudentCollection.Add(s);
StudentCollectionSub.Remove(s);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Experiment
{
public class StudentModel : INotifyPropertyChanged
{
public StudentModel() { }
public int M_STId { get; set; }
public string M_Name { get; set; }
public int M_Maths { get; set; }
public int M_Science { get; set; }
public int M_English { get; set; }
public int M_Total { get; set; }
public StudentModel Copy() => new StudentModel()
{
M_English = this.M_English,
M_Maths = this.M_Maths,
M_Name = this.M_Name,
M_Science = this.M_Science,
M_STId = this.M_STId,
M_Total = this.M_Total
};
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
using System;
using System.Windows.Input;
namespace Experiment
{
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _action;
public RelayCommand(Action<object> action) { _action = action; _canExecute = null; }
public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }
public void Execute(object o) => _action(o);
public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}