c# - Button click doesn't lead to Listview to update it's value -
very simple situation quite stuck here. in past have worked in teams written mvvm architecture codes, literately first little program trying write using mvvm arch. ground up.
i have button fires method called find(). , part works fine. have set binding listview read data find() method going change.
code snippets followed:
public icommand findbluetoothcommand { { if (_findbluetoothcommand == null) { _findbluetoothcommand = new relaycommand(param => find()); } return _findbluetoothcommand; } } public observablecollection<bluetoothdeviceinfo> discovereddeviceslist { { return this._discoverddeviceslist; } set { this._discoverddeviceslist = value; base.onpropertychanged("discovereddeviceslist"); } } private observablecollection<bluetoothdeviceinfo> _discoverddeviceslist; #endregion //presentation properties my find() method
public void find() { bool inrange; guid fakeuuid = new guid("{f13f471d-47cb-41d6-9609-bad0690bf891}"); // specially created value, no matches. var cli = new bluetoothclient(); //bluetoothdeviceinfo[] device = cli.discoverdevices(); //_discoverddeviceslist = new observablecollection<bluetoothdeviceinfo[]>(); _discoverddeviceslist = new observablecollection<bluetoothdeviceinfo>(cli.discoverdevices()); . . . } and xaml code
<window x:class="bluetooth_tester_mvvm.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="blue tooth tester v1.0" height="350" width="525"> <window.resources> <collectionviewsource x:key="devicelist" source="{binding path=discovereddeviceslist}"> </collectionviewsource> </window.resources> <grid> <grid.rowdefinitions> <rowdefinition height="250"></rowdefinition> <rowdefinition height="auto"></rowdefinition> </grid.rowdefinitions> <listview grid.row="0" datacontext="{staticresource devicelist}" itemssource="{binding}"> </listview> <button grid.row="1" content="button" command="{binding path=findbluetoothcommand}" horizontalalignment="right" verticalalignment="top" width="75" margin="5"/> </grid> in process if put find() in viewmodel's constructor can see list of devices. when go way want program work, when button pressed, using break point can see `_discoverddeviceslist' getting updated , filled values doesn't make it's way listview should. seems binding doesn't sense change happened variable.
would nice if give me hand on this.
cheers
instead of
_discoverddeviceslist = new observablecollection<bluetoothdeviceinfo>(cli.discoverdevices()); you should do
discovereddeviceslist= new observablecollection<bluetoothdeviceinfo>(cli.discoverdevices()); otherwise onpropertychanged() not fired.
and better initialize collection once , use clear, add , remove alter it.
discovereddeviceslist.clear() foreach(var item in cli.discoverdevices()) discovereddeviceslist.add(item);
Comments
Post a Comment