Listview中的分组标题重复

时间:2017-03-14 06:05:52

标签: c# wpf listview

Image of header duplication我有一个简单的聊天应用程序。我将所有聊天消息存储在Listview中。我按照发送日期对聊天进行分组。当它首次从数据库中获取时,它会正确显示所有聊天消息的日期,但如果我们添加新的聊天消息以列出相同日期的新消息的分组标题重复。我正在使用绑定到Listview的可观察集合来存储聊天消息。

这是xaml的代码

eventStatus == 1 0r 2

和.cs文件的代码

   if eventSegCtrl.selectedSegmentIndex == 0 {
                myEventCell.eventLabel.text = self.eventScheduleOnc[indexPath.row].eventNameOnc
                let eventDesc = self.eventScheduleOnc[indexPath.row].eventDecOnc

                let eventStatus = eventDesc?.eventStatus
            if eventStatus == 1 {
                myEventCell.eventLabel.textColor = UIColor.green
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                    tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .bottom, animated: true)
                }
            }
            else if eventStatus == 2 {
                myEventCell.eventLabel.textColor = UIColor.red
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                    tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .bottom, animated: true)
                }
            }
        else {
            myEventCell.eventLabel.textColor = UIColor.black
        }
    }

3 个答案:

答案 0 :(得分:0)

您可以尝试启用实时分组:

ICollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(chatList.ItemsSource);
view.GroupDescriptions.Add(new PropertyGroupDescription("Date"));

ICollectionViewLiveShaping liveView = view as ICollectionViewLiveShaping;
if(view != null)
{
    liveView.IsLiveGrouping = true;
    liveView.LiveGroupingProperties.Add("Date");
}

或者,只要添加新项目,您就可以刷新视图:

view.Refresh();

答案 1 :(得分:0)

从评论中我发现OP是按错误的数据类型分组,建议如下:

    //your existing code, I will not break it
    public string Date { get; }

    //change group by property to the following
    public DateTime RealDate
    {
        get
        {
            DateTime dt = Convert.ToDateTime(this.Date).Date;
            return dt;
        }
    }

答案 2 :(得分:0)

在公共声明中添加公共布尔:

/// Public Variable declarations

 public bool ViewSetSwitch = false;

然后在代码行中:

view.GroupDescriptions.Add(new PropertyGroupDescription("Date"));   

添加:

// Checks if ViewSetSwitch is false and if so, runs the command
if (!ViewSetSwitch) view.GroupDescriptions.Add(new PropertyGroupDescription("Date"));

// set ViewSetSwitch to true so the command won't run again causing the headers to add again
ViewSetSwitch=true;

这就是我解决此问题的方法!