UWP - 刷新列表视图和CollectionViewSource

时间:2016-02-06 13:54:16

标签: c xaml listview uwp collectionviewsource

我创建了ChatMessageGroupChatMessageGroupCollection以及ListViewItemsSource设置为CollectionViewSource

<ListView x:Name="ChatMessageLv" ItemsSource="{Binding SelectedChat.ChatMessageGroupCollection.Cvs.View}" ItemTemplateSelector="{StaticResource ChatMessageDataTemplateSelector}">


public class ChatMessageGroup : IGrouping<DateTime, ChatMessage>, INotifyCollectionChanged
    {
        private ObservableCollection<ChatMessage> _chatMessages;
        public DateTime Key { get; set; }

        public ObservableCollection<ChatMessage> ChatMessages
        {
            get { return _chatMessages; }
            set
            {
                if (_chatMessages != null)
                    _chatMessages.CollectionChanged -= CollectionChanged;

                _chatMessages = value;
                _chatMessages.CollectionChanged += CollectionChanged;
            }
        }


        public ChatMessageGroup()
        {
            ChatMessages = new ObservableCollection<ChatMessage>();
        }

        public IEnumerator<ChatMessage> GetEnumerator()
        {
            return ChatMessages.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;
    }


 public class ChatMessageGroupCollection : IEnumerable<ChatMessageGroup>
    {
        private readonly ObservableCollection<ChatMessageGroup> _groups;

        public ObservableCollection<ChatMessage> Source { get; set; }
        public CollectionViewSource Cvs { get; set; }

        public ChatMessageGroupCollection(ObservableCollection<ChatMessage> messages)
        {
            Source = messages;
            messages.CollectionChanged += Messages_CollectionChanged;

            var groups = messages
                .GroupBy(GetGroupKey)
                .Select(x => new ChatMessageGroup()
                {
                    Key = x.Key,
                    ChatMessages = x.OrderBy(GetGroupKey).ToObservableOrDefault()
                })
                .OrderBy(x => x.Key);

            _groups = new ObservableCollection<ChatMessageGroup>(groups);
            Cvs = new CollectionViewSource() { IsSourceGrouped = true, Source = _groups };
        }
...

除了组集合中的更改外,其他所有方法都可以正常工作:

_groups.Add(new ChatMessageGroup()); -> this line reflect changes in ListView

但是,如果我这样做:_groups[0].ChatMessages.Add(new ChatMessage())即使ChatMessageGroup正在实施INotifyCollectionChanged,它也不起作用,每次ChatMessages ObservableCollection都会提出Refresh() }改变了。 解决方法是更新ChatMessages并从_groups中删除组,然后再次添加它,但它不是解决方案。 UWP中没有CollectionViewSource上的UPDATE sm SET sm_fecha_venc = (SELECT MIN(nd_fecha_venc) FROM [db_facturacion].[dbo].[tb_notas_debito] WHERE sm_codigo = nd_num_servicio -- AND nd_referencia = sm_cod_producto AND nd_num_factura = sm_cod_factura AND nd_estado = 'G') ,sm_fecha_ult_pago = (SELECT MAX(nd_fecha_pago) FROM [db_facturacion].[dbo].[tb_notas_debito] WHERE sm_codigo = nd_num_servicio -- AND nd_referencia = sm_cod_producto AND nd_num_factura = sm_cod_factura AND nd_estado = 'C') ,sm_fecha = GETDATE() ,sm_cod_factura_ren = @i_num_factura OUTPUT DELETED.sm_fecha_venc AS FECHA_VENC_OLD, DELETED.sm_fecha_ult_pago AS FECHA_ULT_PAGO_OLD, DELETED.sm_fecha AS FECHA_OLD, DELETED.sm_cod_factura_ren AS COD_FACTURA_REN_OLD INTO @TABLATABLA -- FROM [db_facturacion].[dbo].[tb_servicios] sm --WITH(NOLOCK) INNER JOIN #servicios AS T ON sm_codigo = num_servicio WHERE sm_tipo_bien_protegido = 1 AND [sm_estado] = 1 --AND sm.sm_cod_forma_contrato = 1 AND sm.sm_tipo_inventario = tipo_inventario /*=========================== INSERTED DELETED =============================*/ UPDATE db_facturacion.[dbo].[tb_log_cambio_servicio] SET cs_fecha_venc_old = FECHA_VENC_OLD ,cs_fecha_ult_pago_old = FECHA_ULT_PAGO_OLD ,cs_fecha_old = FECHA_OLD ,cs_cod_factura_ren_old = COD_FACTURA_REN_OLD FROM @TABLATABLA WHERE cs_codigo = @ID_ULTIMO_ING (LAST @@IDENTITY) 。还有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

虽然这在技术上没有资格作为问题的答案,但我认为它可能有资格作为架构答案。

根据WPF中更改的基础集合进行分组列表视图更新的方法就像在XAML中声明的true实例上将IsLiveGroupingRequested设置为CollectionViewSource一样简单。

由于我一直在通过UWP工作(经过近十年的WPF),我得出的结论是,微软通过省略暗示这不是正确的方法。 UWP的问题。因此,自己实现该功能的长度可能被解释为在某种程度上忽略了这一点。

在我的特殊情况下,我决定完全改变我的方法并将渲染实现为ListView的多个实例,而不是将旧范例强加到新平台上。

这样做的结果实际上让我找到了一个改进我的UX以启动的解决方案。

思考的食物......