【问题标题】:Sort Alphabetic ObservableCollection binded to ListBox按字母顺序排序 ObservableCollection 绑定到 ListBox
【发布时间】:2015-07-04 08:45:05
【问题描述】:

我编写了一个小函数,用于使用 API 扫描带有 Virus Total 的文件。它工作得很好,但扫描结果不是按字母顺序排序的。

这是我的代码:

public void init(FileReport _scanResult) {

        try {

            if (_scanResult.ResponseCode == ReportResponseCode.Present) {
                foreach (ScanEngine scan in _scanResult.Scans) {
                    if (scan.Detected == true) {
                        howMany++;
                        _scanResultItems.Add(new ScanResultItems {
                            AvName = scan.Name,
                            AvResult = new Uri("/Images/inWatch.avNOT.png", UriKind.RelativeOrAbsolute),
                            AvStatus = "BEDROHUNG!"
                        });
                        Width = 390;
                    }
                    else {
                        _scanResultItems.Add(new ScanResultItems {
                            AvName = scan.Name,
                            AvResult = new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
                            AvStatus = "OK"
                        });
                    }
                }

                lstScanResults.ItemsSource = _scanResultItems.OrderBy(item => item.AvName).ToList();
            }
        }
        catch(Exception ex) {
            GeneralSettings.LogException(ex);
        }

感谢您的回答!

【问题讨论】:

标签: c# wpf listbox observablecollection


【解决方案1】:

您可以使用 System.Linq 对结果集中的项目进行排序; OrderBy() 方法,像这样:

_scanResultItems = _scanResultItems.OrderBy(item => item.Name).ToList();

【讨论】:

  • 感谢您的出色回答,它就像魅力一样!我更新了我的帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2011-01-30
  • 2019-04-05
相关资源
最近更新 更多