c# - ListView not updating on CollectionChanged -
i starting play realm, , trying bind collection realm database listview. binding works fine, listview not update when adding new items. understanding irealmcollection<> implements inotifycollectionchanged , inotifypropertychanged events.
here simple application reproduce issue:
view:
<page x:class="app3.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="using:app3" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d"> <grid background="{themeresource applicationpagebackgroundthemebrush}"> <stackpanel> <button click="buttonbase_onclick" content="add" /> <listview x:name="listview"> <listview.itemtemplate> <datatemplate> <textblock text="{binding id}" /> </datatemplate> </listview.itemtemplate> </listview> </stackpanel> </grid> </page> codebehind:
namespace app3 { public class thing : realmobject { public string id { get; set; } = guid.newguid().tostring(); } /// <summary> /// empty page can used on own or navigated within frame. /// </summary> public sealed partial class mainpage : page { private realm _realm; private irealmcollection<thing> things; public mainpage() { this.initializecomponent(); _realm = realm.getinstance(); things = (irealmcollection<thing>)_realm.all<thing>(); listview.itemssource = things; } private void buttonbase_onclick(object sender, routedeventargs e) { _realm.write(() => { var thing = new thing(); _realm.add(thing); }); } } } i use mvvm (template10), simple application demonstrate issue. clicking add button adds item database, listview updates when application first loaded. have read similar questions, have not been able find answer works yet. inverse relationships , ui-update not working? closest have found yet, still not fix issue.
edit
i can force rebind so:
listview.itemssource = null; listview.itemssource = things; but not optimal. trying take advantage of realm's "live objects" collection should know when items changed or added.
edit 2
setting bindingmode=oneway in code-behind not change behavior:
_realm = realm.getinstance(); things = (irealmcollection<thing>)_realm.all<thing>(); var binding = new binding { source = things, mode = bindingmode.oneway }; listview.setbinding(listview.itemssourceproperty, binding); solution
it turned out known issue in irealmcollection: https://github.com/realm/realm-dotnet/issues/1461#issuecomment-312489046 fixed in realm 1.6.0. have updated pre-release nuget package , can confirm listview updates expected.
set mode=oneway in binding
method 1: in xaml
<listview itemssource="{x:bind things, mode=oneway}" /> method 2: in code behind
binding mybind = new binding(); mybind.source = things; mybind.mode = bindingmode.oneway; mylistview.setbinding(listview.itemssourceproperty, mybind); it bug in irealmcollection. can use prerelease nuget solve it.
for more info:
irealmcollection not update uwp listview
github issue: irealmcollection not update uwp listview
Comments
Post a Comment