Hi, I'm trying to implement an "undo" button(RevertChanges method) in my program.
Everything seems to work fine, but when I call the RevertChanges void (It´s binded to a button), my LINQ2SQL object is correctly reverted, but the DataGrid control doesn't get updated.
Only when I click twice a cell or force to refresh (Using datagrid.Items.Refresh()) the content of the DataGrid take the actual value.
I'm new programming C#, so maybe it's just a basic mistake.
The LINQtoSQL class was generater automatically.
My ViewModel class:
public class ViewModel : INotifyPropertyChanged{
public static DataClasses1DataContext DContext1 = new DataClasses1DataContext();
public ViewModel()
{
Model Model1 = new Model();
tblToView = Model1.GetList(DContext1);
var query = from a in Model1.GetList(DContext1) select a;
ListTest1 = new ObservableCollection<utblTestTable>(query.ToList());
}
public ViewModel(int itemNo)
{
Model Model1 = new Model();
tblToView = Model1.GetList(DContext1,itemNo);
}
public ObservableCollection<utblTestTable> ListTest1 {get;set;}
public IEnumerable<utblTestTable> tblToView;
private RelayCommand updatecmd1;
public RelayCommand UpdateCMD1
{
get
{
updatecmd1 = new RelayCommand(a => UpdateVoid1());
return updatecmd1;
}
}
private RelayCommand newwindowcmd1;
public RelayCommand NewWindowCMD1
{
get
{
newwindowcmd1 = new RelayCommand(a => NewWindow());
return newwindowcmd1;
}
}
private RelayCommand savecmd1;
public RelayCommand SaveCMD1
{
get
{
savecmd1 = new RelayCommand(a => RevertChanges(a));
return savecmd1;
}
}
private void UpdateVoid1()
{
DContext1.SubmitChanges();
}
private void RevertChanges(object parameter)
{
DContext1.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DContext1.utblTestTables);
}
private int item;
public utblTestTable Item
{
set
{
item = value.ID;
}
}
private void NewWindow()
{
ViewModel VMPopUp = new ViewModel(item);
ViewPopup1 PupUpView = new ViewPopup1();
PupUpView.DataContext = this;
PupUpView.dGridPopup.ItemsSource = VMPopUp.tblToView;
PupUpView.ShowDialog();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
And the XAML code ot the DataGrid binding:
<DataGrid ItemsSource="{Binding ListTest1, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" CanUserResizeRows="False" SelectedItem ="{Binding Item , UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource PlaniTDGrid}" Name="dGrid1" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="386" Width="772">