This is my DataGrid testing code. I set some data on the DataGrid source with ObservableCollection and bound this. And I modify the value where the ObservableCollection member's property. In this case, my UI has to show that value is changed. However, my DataGrid only interact when I selected the cell.
Binding
public ObservableCollection<MyClass> griddata { get; set; } = new ObservableCollection<MyClass>();
My Class
public class MyClass
{
public int num { get; set; }
public int idxnumber { get; set; }
}
XAML
<Grid>
<DataGrid ItemsSource="{Binding griddata, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</DataGrid>
</Grid>
Function
//... griddata.add(someOfData);
public void ViewModel()
{
int i = 0;
while(i < 30)
{
testing(0);
i++;
}
}
private void testing(int idxnum)
{
var test = griddata.Where(z => z.idxnumber == idxnum).FirstOrDefalut();
test.num += 1;
}
Result
Cell value shows that 0
and I selected value is changed to 30
, immediately.
Expect result
Cell value shows 0
to 30
continuously.
EDIT :
This is my whole code:
XAML
<Window x:Class="TestSol3.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:TestSol3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding griddata}">
</DataGrid>
</Grid>
</Window>
Model.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestSol3
{
public class Model
{
public int num { get; set; }
public int idxnumber { get; set; }
}
}
ViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TestSol3
{
public class ViewModel
{
public ObservableCollection<Model> griddata { get; set; } = new ObservableCollection<Model>();
public ViewModel()
{
griddata.Add(new Model() { num = 0, idxnumber = 0 });
griddata.Add(new Model() { num = 1, idxnumber = 1 });
Load(0);
}
private void Load(int idxnum)
{
int i = 0;
while(i < 30)
{
i++;
griddata[idxnum].num++;
//Thread.Sleep(200);
}
}
}
}