I tried several suggested solutions now, but I'm stuck.
I have a WPF MVVM application, which should list all strings passed through a UDP Client and updates it.
I dont know why but my collection gets updated and the Listview does not update.The code works fine, as long as I add values somehow through application code (button click) or a timer which adds a message each second.
The weird thing now is, that if I do both simultaneously, only the timer messages are shown in the ListView, but NOT the UDP messages, although both are in the ObserveableCollection
Here is a Screenshot of the application on the right and the Collection on the left.MessageXX are from the timer which represent each second. All others are added through UDP Update
To make it easier I switched my LogEvent Object to a simple ObserveableCollection type, but nothing changed.The AddLogEntry method is called from the UDP Client and the Timer.
This is how I initialize the UDP Client
private UdpClient GetUDPClient() { if(udpClient == null) { udpClient = new UdpClient(); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpClient.Client.Bind(getIPEndPoint()); } return udpClient; }
This is the method to start the update and update the collection (from UDP and timer)
private async void StartListeningAsync() { while (true) { UdpReceiveResult result = await udpClient.ReceiveAsync(); string message = Encoding.UTF8.GetString(result.Buffer); await Application.Current.Dispatcher.InvokeAsync(() => { AddLogEntry(message); }); } } private async void AddLogEntry(string message) { await Application.Current.Dispatcher.InvokeAsync(() => { Debug.WriteLine(message); LogEvent logEvent = new LogEvent(message); LogEntries.Add(logEvent.Message); OnPropertyChanged(nameof(LogEntries)); var listView = Application.Current.MainWindow.FindName("ListView") as System.Windows.Controls.ListView; if (AutoScroll && LogEntries.Any()) { listView?.ScrollIntoView(LogEntries.Last()); } }); }
This is the ListView Part
<ListView ItemsSource="{Binding LogEntries}" SelectedItem="{Binding SelectedLogEntry}" Name="ListView"></ListView>
Any ideas what could cause this problem or what I could try?
Thank you very much