DataGrid не обновляет объект вне модели представления ⇐ C#
-
Гость
DataGrid не обновляет объект вне модели представления
I'm encountering an issue of DataGrid and I tried whole day not found the solution. Here I have a DataViewModel:
public ObservableCollection Source { get; } = new ObservableCollection(); public void AddTemp(Temp temp) { Source.Add(temp); } which binded its dataContext to the view, contain a datagrid:
and a AddViewModel:
public DataViewModel DataViewModel { get; set;} public AddViewModel(Cami cami) { DataViewModel = new DataViewModel(); } public async Task Cap() { await Application.Current.Dispatcher.Invoke(async () => { //... if (timer == null) { timer = new System.Timers.Timer(1000); timer.Elapsed += async (sender, e) => await GetRealTimeDataAsync(); timer.Start(); } //... }); } private async Task GetRealTimeDataAsync() { var (min, max) = await Cam.F.GetRealTimeTemp(); var temp = new Temp { id = id, Time = DateTime.Now, Min = min, Max = max, }; DataViewModel.AddTemp(temp); return temp; } Im using MVVM.CommunityToolkit. The problem is, when i debug, i see the Source in the DataViewModel actually updated. But the datagrid not update its row. Nothing happen. But when i create Temp object inside the DataViewModel and update it via a button which binded RelayCommand, it works. But i need the data must be come from the AddViewModel, not the DataViewModel itself. I tried so many way, pass by constructor but it not help. Any explain and help is appreciated. Thanks!
Источник: https://stackoverflow.com/questions/781 ... view-model
I'm encountering an issue of DataGrid and I tried whole day not found the solution. Here I have a DataViewModel:
public ObservableCollection Source { get; } = new ObservableCollection(); public void AddTemp(Temp temp) { Source.Add(temp); } which binded its dataContext to the view, contain a datagrid:
and a AddViewModel:
public DataViewModel DataViewModel { get; set;} public AddViewModel(Cami cami) { DataViewModel = new DataViewModel(); } public async Task Cap() { await Application.Current.Dispatcher.Invoke(async () => { //... if (timer == null) { timer = new System.Timers.Timer(1000); timer.Elapsed += async (sender, e) => await GetRealTimeDataAsync(); timer.Start(); } //... }); } private async Task GetRealTimeDataAsync() { var (min, max) = await Cam.F.GetRealTimeTemp(); var temp = new Temp { id = id, Time = DateTime.Now, Min = min, Max = max, }; DataViewModel.AddTemp(temp); return temp; } Im using MVVM.CommunityToolkit. The problem is, when i debug, i see the Source in the DataViewModel actually updated. But the datagrid not update its row. Nothing happen. But when i create Temp object inside the DataViewModel and update it via a button which binded RelayCommand, it works. But i need the data must be come from the AddViewModel, not the DataViewModel itself. I tried so many way, pass by constructor but it not help. Any explain and help is appreciated. Thanks!
Источник: https://stackoverflow.com/questions/781 ... view-model
Мобильная версия