Reputation: 466
I am working with mvvmLight-framework. I got two usercontrols.
One usercontrol (objectInspector) is for saving data:
<Button Command="{Binding ObjectModel.OkCommand}" />
It is bound to the view-model "objectInspectorViewModel".
The other usercontrol (outliner) is for loading/presenting all of the data
<DataGrid ItemsSource="{Binding Path=ObjectModels, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="0"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="ID"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Length}" Header="Length"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Height}" Header="Height"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Width}" Header="Width"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Type}" Header="Type"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
It is bound to the viewmodel "outlinerViewModel".
Loading and saving works fine. But what i want is to refresh the datagrid after saving a new object.
My OutlinerViewModel looks like this:
public class OutlinerViewModel : BaseViewModel
{
public List<ObjectModel> ObjectModels { get; set; }
public OutlinerViewModel()
{
string file = $@"{Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName}\DataSource\objects.csv";
ObjectModels = ReadFile(file);
}
[...]
}
My ObjectInspectorViewModel looks like this:
public class ObjectInspectorViewModel : BaseViewModel
{
public ObjectModel ObjectModel { get; set; } = new ObjectModel();
}
And this is the method for saving a new object to the ''database'' from the ObjectModel:
public RelayCommand OkCommand { get; private set; }
protected override void InitCommands()
{
base.InitCommands();
OkCommand = new RelayCommand(
() =>
writeToCsv(
$@"{Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName}\DataSource\objects.csv",
this.ToString()),
() => IsOk);
}
How to update dataGrid after saving data with MvvmLight?
Upvotes: 0
Views: 387
Reputation: 201
Personnaly, I like to have my view up to date with the data in the database. So when I save an element, I refresh the datagrid by retrieving the data from there. This way, you don't have synchronizing issue. If performance is important, you can try to update only the element you saved. So in your case, i'd just call the "readFile" method after saving an object.
By the way, all your ObjectModel properties should call RaisePropertyChanged in your ViewModel:
Something like this:
private long IdProperty;
public long Id
{
get { return IdProperty; }
set { IdProperty = value; RaisePropertyChanged(() => Id); }
}
private long NameProperty;
public long Name
{
get { return NameProperty; }
set { NameProperty = value; RaisePropertyChanged(() => Name); }
}
This way, all the properties are updated in the view when they're modified. (RaisePropertyChanged comes with the ViewModelBase class)
Upvotes: 2
Reputation: 3723
Change your List<ObjectModel>
to an ObservableCollection<ObjectModel>
.
It should be that simple.
Update
Also, raise PropertyChanged
.
private ObservableCollection<ObjectModel> objectModels;
public ObservableCollection<ObjectModel> ObjectModels
{
get
{
return onjectModels;
}
set
{
objectModels = value;
OnPropertyChanged();
}
}
Assuming your view model implements INotifyPropertyChanged. It could be implemented like this.
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName=null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Upvotes: 1