记WPF ComboBox数据源更新后无法即时刷新UI的问题(已解决)

最近在做动态刷新COM串口列表的小功能,发现在UI线程异步处理ComboBox数据源后,UI无法即时刷新的问题。调试代码发现数据源是改变了的,但是无意中使用另外一种方式却能够有很好的效果,原因暂时未知。

问题解决方法:使用SelectedIndex代替SelectedValue,参考《I cant change the combobox selection programmatically WPF C#》。

开发环境为:VS2019 + WPF(.Net5) + Prism。

首先,在View中为ComboBox绑定数据源:

<ComboBox Grid.Row="0" Grid.Column="1" Margin="5,5,0,5"
          ItemsSource="{Binding SerialPortList}"
          SelectedItem="{Binding SelectedPortName}"/>

其次,在View的Loaded事件中异步处理COM串口变化:

private async void OnLoadedAsync(object sender, RoutedEventArgs e)
{
    if (_viewModel != null)
    {
        await _viewModel.StartMonitorSerialPortAsync();
    }
}

然后,在ViewModel中监视COM串口是否发生变化:

public async Task StartMonitorSerialPortAsync()
{
    do
    {
        if (_monitorSerialPort)
        {
            var portNames = _serialPortService.GetPortNames();
            var updateFlag = portNames.Length != SerialPortList.Count;
            updateFlag = updateFlag || SerialPortList.FirstOrDefault(t => !portNames.Contains(t)) != null;
            updateFlag = updateFlag || portNames.FirstOrDefault(t => !SerialPortList.Contains(t)) != null;
            if (updateFlag)
            {
                Application.Current?.Dispatcher.BeginInvoke(new Action(() =>
                {
                    SerialPortList.Clear();
                    SerialPortList.AddRange(portNames);
                    SelectedPortName = SerialPortList.FirstOrDefault(t => t == SelectedPortName);
                }));
            }
        }

        await Task.Delay(1000, _tokenSource.Token);
    } while (!_tokenSource.IsCancellationRequested);
}

程序运行起来之后,重复拔插USB串口线ComboBox数据无变化。

之后,修改代码,在ViewModel(构造函数)中开启线程处理COM串口变化:

Task.Run(async () => await StartMonitorSerialPortAsync(), _tokenSource.Token);

再次运行程序,ComboBox已经能够在拔插USB串口线的过程中改变数据了。

在找出具体原因之前,这是一个比较奇怪的问题。我的猜测是,在View的Loaded事件中处理方法中,无论是异步还是同步处理,对ObservableCollection集合的修改不会触发通知机制。

如果在按钮事件处理方法中修改ObservableCollection集合,ComboBox控件也能够即时刷新UI。

posted @ 2022-07-11 21:32  xhubobo  阅读(1210)  评论(0编辑  收藏  举报