I have a problem with binding data in WPF. Task runed in constructor work perfect. But when i tried to run task in timer handler it didnt work -> the data in view didnt update...
public ObservableCollection<ushort> Test2{get; set;}
public Settings()
{
InitializeComponent();
Test2 = new ObservableCollection<ushort>();
Test2.Add(666);
Test2.Add(111);
Task.Run(async () =>
{
int i = 0;
while (true)
{
await Task.Delay(500);
Test2[1] += (ushort)2;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Test2)));
}
});
}
private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
Task.Run(async () =>
{
int i = 0;
while (true)
{
await Task.Delay(500);
Test2[1] += (ushort)2;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Test2)));
}
});
}
Timer
oTimer.Interval = 1000;
oTimer.Elapsed += OnTimedEvent;
oTimer.AutoReset = true;
oTimer.Enabled = true;
oTimer.Start();
Other class file
public UserControlHome()
{
InitializeComponent();
DataContext = new Settings();
}
And the XML file
<TextBlock x:Name="Tob2Sensor1" Grid.Column="1" Text="{Binding Test2[1]}" HorizontalAlignment="Center"/>