A vkontakte chat is being developed that supports media attachments. A reversed ListView was written that supports data virtualization and uses the ISupportIncrementalLoading interface. Receiving messages occurs over the network from api vkontakte. The message template contains a control that deals with the creation of media attachments (documents, photos, videos, products, etc.).
The problem is this: When a message first appears, attachments, if any, are displayed well. However, if you scroll further or go into a conversation with another person, then these same attachments may appear in messages in which they should not be. What can this happen? I sin on this very virtualization. If you use direct access data virtualization, will the problem disappear? The same error occurs when building posts on the wall. Those. In the new elements appear pieces of previously viewed records.
Code
<DataTemplate x:DataType="models:Message" x:Key="InPrivateMessageTemplate"> <attachments:MessagesAttachmentsController Attachments="{x:Bind Attach}" /> </DataTemplate> // what creates attachments.
public class MessagesAttachmentsController : Control { private StackPanel _layouRoot; private MediaItemPresenter _mediaItemPresenter; public MessagesAttachmentsController() { DefaultStyleKey = typeof(MessagesAttachmentsController); } public MediaItemPresenter MediaItemPresenter => _mediaItemPresenter ?? (_mediaItemPresenter = new MediaItemPresenter()); public List<IAttachment> Attachments { get => (List<IAttachment>)GetValue(AttachmentsProperty); set => SetValue(AttachmentsProperty, value); } public static readonly DependencyProperty AttachmentsProperty = DependencyProperty.Register(nameof(Attachments), typeof(List<IAttachment>), typeof(MessagesAttachmentsController), new PropertyMetadata(null, (s, e) => { if (Equals(e.NewValue, e.OldValue) || e.NewValue == null) { return; } (s as MessagesAttachmentsController)?.ReBuild(); })); protected override void OnApplyTemplate() { base.OnApplyTemplate(); _layouRoot = GetTemplateChild("LayoutRoot") as StackPanel; ReBuild(); } private void ReBuild() { if (_layouRoot == null || Attachments == null) { return; } _layouRoot.Children.Clear(); foreach (var item in Attachments) { switch (item) { case Core.Entity.Attachments.Sticker sticker: { CreateStiсker(sticker); return; } case Core.Entity.Attachments.Link link: { CreateLink(link); return; } case IAttachmentPreview ap: { MediaItemPresenter.Attachments.Add(ap); break; } default: break; } } if (_mediaItemPresenter != null) { _layouRoot.Children.Add(_mediaItemPresenter); } } private void CreateStiсker(Core.Entity.Attachments.Sticker sticker) { var s = new Sticker { PhotoUrl = sticker.Photo128, Size = 128 }; _layouRoot.Children.Add(s); } private void CreateLink(Core.Entity.Attachments.Link link) { var l = new Link { PhotoUrl = link.Photo?.Photo604, Title = link.Title, Description = link.Description, Url = link.Url }; _layouRoot.Children.Add(l); } }