c# - Update Listview with view model with button click -
i want able update listview when user clicks on listview item (row) , clicks button. here code:
xaml
<window x:class="cs3000config2.dialogs.clone" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:cs3000config2" title="clone scanner configuration" height="768" width="1024" fontsize="16" windowstyle="toolwindow" windowstartuplocation="centerscreen" resizemode="noresize" loaded="window_loaded"> <window.resources> <local:scannerviewmodel x:key="viewmodel" /> </window.resources> <window.background> <lineargradientbrush endpoint=".5,1" startpoint=".5,0"> <gradientstop color="#b8e1fc" offset="0" /> <gradientstop color="#a9d2f3" offset="0.10" /> <gradientstop color="#90bae4" offset="0.25" /> <gradientstop color="#90bcea" offset="0.37" /> <gradientstop color="#90bff0" offset=".50" /> <gradientstop color="#6ba8e5" offset=".51" /> <gradientstop color="#a2daf5" offset=".83" /> <gradientstop color="#bdf3fd" offset="1" /> </lineargradientbrush> </window.background> <dockpanel> <grid> <dockpanel height="650" margin="50,40,50,0" width="800" verticalalignment="top" background="white"> <grid> <toolbar height="46" margin="0,6,-11,0" name="toolbar1" verticalalignment="top" toolbartray.islocked="true" borderbrush="black" borderthickness=".5" loaded="toolbar1_loaded"> <button toolbar.overflowmode="asneeded" click="button_click" name="btnloadtoselected"> <stackpanel orientation="horizontal" verticalalignment="center" > <stackpanel.resources> <style targettype="{x:type textblock}"> <setter property="margin" value="10,0,0,0"/> </style> </stackpanel.resources> <image source="/cs3000config2;component/resources/dsave.png" /> <textblock verticalalignment="center" fontsize="16" margin="5,0,0,0">load selected scanners</textblock> </stackpanel> </button> <separator /> <button toolbar.overflowmode="never" click="button_click_2" name="btnloadtoall"> <stackpanel orientation="horizontal" verticalalignment="center" > <stackpanel.resources> <style targettype="{x:type textblock}"> <setter property="margin" value="10,0,0,0"/> </style> </stackpanel.resources> <image source="/cs3000config2;component/resources/document-save-all2.png" /> <textblock verticalalignment="center" fontsize="16" margin="5,0,0,0"> load scanners</textblock> </stackpanel> </button> <separator /> <button toolbar.overflowmode="never" click="button_click_1"> <stackpanel orientation="horizontal" verticalalignment="center" > <stackpanel.resources> <style targettype="{x:type textblock}"> <setter property="margin" value="10,0,0,0"/> </style> </stackpanel.resources> <image source="/cs3000config2;component/resources/refresh.gif" /> <textblock verticalalignment="center" fontsize="16" margin="5,0,0,0"> refresh scanner list</textblock> </stackpanel> </button> <separator /> </toolbar> <scrollviewer margin="6.5,58,6,6" horizontalscrollbarvisibility="auto" verticalscrollbarvisibility="auto" > <grid datacontext="{binding source={staticresource viewmodel}}" name="grid1"> <listview margin="0,5" name="listview1" selectionmode="multiple" itemssource="{binding scanners}"> <listview.view> <gridview> <gridviewcolumn header="" width="100"> <gridviewcolumn.celltemplate> <datatemplate> <image height="50" source="/cs3000config2;component/resources/cs3000-small.png" /> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn header="drive" width="50" displaymemberbinding="{binding drive}" /> <gridviewcolumn header="model" width="100" displaymemberbinding="{binding model}" /> <gridviewcolumn header="serial" width="200" displaymemberbinding="{binding serial}" /> <gridviewcolumn header="" width="325" > <gridviewcolumn.celltemplate> <datatemplate> <progressbar name="prog1" visibility="{binding isvisible, mode=twoway, updatesourcetrigger=propertychanged}" maximum="{binding maxprogress, mode=twoway, updatesourcetrigger=propertychanged}" value="{binding progress, mode=twoway, updatesourcetrigger=propertychanged}" height="20" width="200" /> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> </gridview> </listview.view> </listview> </grid> </scrollviewer> </grid> </dockpanel> </grid> </dockpanel>
view model (scannerviewmodel.cs)
public class scannerviewmodel : inotifypropertychanged { private string[] scanners; observablecollection<scanner> items = new observablecollection<scanner>(); private arraylist drivelist = new arraylist(); public scannerviewmodel() { getdrives(); scanners = (string[])drivelist.toarray(typeof(string)); if (scanners.length <= 0) { return; } else { foreach (string scnr in scanners) { config c = new config(); c.loadsysinfo(scnr + "\\parameters\\sysinfo.txt"); progressbar progbar = new progressbar(); progbar.height = 20; progbar.width = 150; items.add(new scanner() { drive = scnr, model = c.model, serial = c.serial, maxprogress = 10000, progress = 0, isvisible= visibility.hidden }); } } } private void getdrives() { drivelist.clear(); foreach (driveinfo info in driveinfo.getdrives()) { if (info.drivetype == drivetype.removable) { if (directory.exists(info.rootdirectory.tostring() + "parameters")) { drivelist.add(info.rootdirectory.tostring()); } } } } public observablecollection<scanner> scanners { { return items; } set { items = value; raisepropertychanged("scanners"); } } public event propertychangedeventhandler propertychanged; private void raisepropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } } }
model class (scanner.cs)
public class scanner { public string model { get; set; } public string serial { get; set; } public visibility isvisible { get; set; } public string drive { get; set; } public int32 progress { get; set; } public int32 maxprogress { get; set; } } }
can tell me how update 1 of listview rows , make progress bar visible if user clicks on row in list view , clicks load scanner button? sample button click code appreciated.
try this
1- in vm create list hold selected items eg observablecollection selectedscanners {get;set;} 2- in xaml, bind selecteditem of listview above 3- create icommand buttons
public icommand updatescannerscommand { { return new relaycommand(()=> { ... update code here } } }
4- remove visibility property , use boolean instead, (and use convertor in xaml determine visibility)
5
Comments
Post a Comment