c# - Multiselect listview with background change not working for iOS -
i have custom control perform multiselect on listview.
viewcell code:
using system; using system.collections.generic; using system.componentmodel; using system.globalization; using system.linq; using system.reflection.emit; using system.text; using xamarin.forms; namespace ____ { public class selectmultiplebasepage<t> : contentpage { public class wrappedselection<t> : inotifypropertychanged { public t item { get; set; } bool isselected = false; public bool isselected { { return isselected; } set { if (isselected != value) { isselected = value; propertychanged(this, new propertychangedeventargs("isselected")); // propertychanged (this, new propertychangedeventargs (nameof (isselected))); // c# 6 } } } public event propertychangedeventhandler propertychanged = delegate { }; } public class backgroundcolorconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { if (value bool) { if ((bool)value) { return color.fromrgb(214, 221, 228); } else { return color.white; } } else { return color.white; } } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } } public class wrappeditemselectiontemplate : viewcell { public wrappeditemselectiontemplate() : base() { grid objgrid = new grid(); objgrid.rowdefinitions.add(new rowdefinition { height = new gridlength(1, gridunittype.star) }); // objgrid.columndefinitions.add(new columndefinition { width = new gridlength(75, gridunittype.absolute), }); objgrid.columndefinitions.add(new columndefinition { width = new gridlength(1, gridunittype.star) }); objgrid.columndefinitions.add(new columndefinition { width = gridlength.auto }); // // column 1:- image objimage = new image(); objimage.setbinding(image.sourceproperty, new binding("item.image")); objgrid.children.add(objimage, 0, 0); objgrid.padding = new thickness(10); // // column 2:- stacklayout objstacklayoutcol2 = new stacklayout(); objgrid.children.add(objstacklayoutcol2, 1, 0); label name = new label() { text = "name", style = (style)application.current.resources["labelstyle"] , }; label date = new label() { text = "date", style = (style)application.current.resources["labelstyletiny"] }; name.setbinding(label.textproperty, new binding("item.name")); date.setbinding(label.textproperty, new binding("item.date")); objstacklayoutcol2.children.add(name); objstacklayoutcol2.padding = new thickness(10,10,10,10); objstacklayoutcol2.children.add(date); objgrid.setbinding(grid.backgroundcolorproperty, "isselected", converter: new backgroundcolorconverter()); // var moreaction = new menuitem { text = "more" }; moreaction.setbinding(menuitem.commandparameterproperty, new binding(".")); moreaction.clicked += (sender, e) => { var mi = ((menuitem)sender); }; var deleteaction = new menuitem { text = "delete", isdestructive = true }; // red background deleteaction.setbinding(menuitem.commandparameterproperty, new binding(".")); deleteaction.clicked += (sender, e) => { var mi = ((menuitem)sender); }; contextactions.add(moreaction); contextactions.add(deleteaction); view = objgrid; } } public list<wrappedselection<t>> wrappeditems = new list<wrappedselection<t>>(); public selectmultiplebasepage(list<t> items) { wrappeditems = items.select(item => new wrappedselection<t>() { item = item, isselected = false }).tolist(); listview mainlist = new listview() { itemssource = wrappeditems, itemtemplate = new datatemplate(typeof(wrappeditemselectiontemplate)), }; mainlist.itemselected += (sender, e) => { if (e.selecteditem == null) return; var o = (wrappedselection<t>)e.selecteditem; o.isselected = !o.isselected; ((listview)sender).selecteditem = null; //de-select }; content = mainlist; if (device.os == targetplatform.winphone) { // fix issue rows badly sized (as tall screen) on winphone8.1 mainlist.rowheight = 40; // need icons windows app bar (other platforms can use text) toolbaritems.add(new toolbaritem("all", "check.png", selectall, toolbaritemorder.primary)); toolbaritems.add(new toolbaritem("none", "cancel.png", selectnone, toolbaritemorder.primary)); } else { mainlist.rowheight = 60; toolbaritems.add(new toolbaritem("all", null, selectall, toolbaritemorder.primary)); toolbaritems.add(new toolbaritem("none", null, selectnone, toolbaritemorder.primary)); } } void selectall() { foreach (var wi in wrappeditems) { wi.isselected = true; } } void selectnone() { foreach (var wi in wrappeditems) { wi.isselected = false; } } public list<t> getselection() { return wrappeditems.where(item => item.isselected).select(wrappeditem => wrappeditem.item).tolist(); } } }
this code works android. selects multiple rows per requirement, same code not work ios. on ios background change not work expected. select rows on single tap , on double tap. not sure wrong.
can me resolve issue? or suggest other way achieve multi-select feature.
i managed working adding tick feature.
here how did it:
using system; using system.collections.generic; using system.componentmodel; using system.globalization; using system.linq; using system.reflection.emit; using system.text; using xamarin.forms; namespace ---- { public class selectmultiplebasepage<t> : contentpage { public delegate void imageselectedhandler(object sender, eventargs e); public event imageselectedhandler onimageselected; public class wrappedselection<t> : inotifypropertychanged { public t item { get; set; } bool isselected = false; public bool isselected { { return isselected; } set { if (isselected != value) { isselected = value; propertychanged(this, new propertychangedeventargs("isselected")); // propertychanged (this, new propertychangedeventargs (nameof (isselected))); // c# 6 } } } public event propertychangedeventhandler propertychanged = delegate { }; } public class tickconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { if (value bool) { if ((bool)value) { return device.onplatform("images/checked_checkbox.png", "checked_checkbox.png", "images/checked_checkbox.png"); } else { return device.onplatform("images/unchecked_checkbox.png", "unchecked_checkbox.png", "images/unchecked_checkbox.png"); } } else { return device.onplatform("images/unchecked_checkbox.png", "unchecked_checkbox.png", "images/unchecked_checkbox.png"); } } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } } public class wrappeditemselectiontemplate : viewcell { private readonly imageselectedhandler _parenthandler; static int = 0; public wrappeditemselectiontemplate(imageselectedhandler parenthandler) : base() { _parenthandler = parenthandler; list<listitems> items = new list<listitems>(); if (application.current.properties.containskey("selectedpage")) { if (application.current.properties["selectedpage"].tostring() == "recording") { items = recordinglistpage.items; } else { items = recordingdetailspage.items; } } grid objgrid = new grid(); // objgrid.rowdefinitions.add(new rowdefinition { height = new gridlength(1, gridunittype.star) }); objgrid.columndefinitions.add(new columndefinition { width = new gridlength(75, gridunittype.absolute), }); objgrid.columndefinitions.add(new columndefinition { width = new gridlength(1, gridunittype.star) }); objgrid.columndefinitions.add(new columndefinition { width = new gridlength(75, gridunittype.absolute), }); // column 1:- image objimage = new image(); objimage.setbinding(image.sourceproperty, new binding("item.image")); objgrid.children.add(objimage, 0, 0); stacklayout objstacklayoutcol2 = new stacklayout(); objgrid.children.add(objstacklayoutcol2, 1, 0); label name = new label() { text = "name", style = (style)application.current.resources["labelstyle"], }; label date = new label() { text = "date", style = (style)application.current.resources["labelstyletiny"] }; name.setbinding(label.textproperty, new binding("item.name")); date.setbinding(label.textproperty, new binding("item.date")); objstacklayoutcol2.children.add(name); objstacklayoutcol2.children.add(date); objstacklayoutcol2.padding = new thickness(10); image objimageview = new image(); objimageview.source = device.onplatform("icons/ic_mode_edit.png", "search.png", "images/search.png");//imagesource.fromfile("search.png"); stacklayout stv = new stacklayout(); stv.children.add(objimageview); stv.padding = new thickness(10); stv.horizontaloptions = layoutoptions.center; stv.verticaloptions = layoutoptions.center; objimageview.setbinding(image.sourceproperty, "isselected", converter: new tickconverter()); objimage.styleid = items[i].id.tostring(); i++; if (i == items.count) { = 0; } var tapgesturerecognizer = new tapgesturerecognizer(); tapgesturerecognizer.tapped += onimagebtntapped; objimage.gesturerecognizers.add(tapgesturerecognizer); objgrid.children.add(stv, 2, 0); var moreaction = new menuitem { text = "more" }; moreaction.setbinding(menuitem.commandparameterproperty, new binding(".")); moreaction.clicked += (sender, e) => { var mi = ((menuitem)sender); }; var deleteaction = new menuitem { text = "delete", isdestructive = true }; // red background deleteaction.icon = device.onplatform("icons/cancel.png", "cancel.png", "images/cancel.png"); deleteaction.setbinding(menuitem.commandparameterproperty, new binding(".")); deleteaction.clicked += (sender, e) => { var mi = ((menuitem)sender); }; // // add context actions cell // contextactions.add(moreaction); contextactions.add(deleteaction); stacklayout st = new stacklayout(); st.children.add(objgrid); st.children.add(new boxview() { color = color.fromhex("#a4b3c1"), widthrequest = 100, heightrequest = 1 }); view = st; } private void onimagebtntapped(object sender, eventargs e) { //... _parenthandler.invoke(sender, e); } } public static list<wrappedselection<t>> wrappeditems = new list<wrappedselection<t>>(); public selectmultiplebasepage(list<t> items) { wrappeditems = items.select(item => new wrappedselection<t>() { item = item, isselected = false }).tolist(); listview mainlist = new listview() { itemssource = wrappeditems, itemtemplate = new datatemplate(() => new wrappeditemselectiontemplate(handleimageselected)), }; mainlist.itemselected += (sender, e) => { if (e.selecteditem == null) return; var o = (wrappedselection<t>)e.selecteditem; o.isselected = !o.isselected; ((listview)sender).selecteditem = null; //de-select }; content = mainlist; mainlist.hasunevenrows = true; if (device.os == targetplatform.winphone) { mainlist.rowheight = 40; toolbaritems.add(new toolbaritem("all", "check.png", selectall, toolbaritemorder.primary)); toolbaritems.add(new toolbaritem("none", "cancel.png", selectnone, toolbaritemorder.primary)); } else { toolbaritems.add(new toolbaritem("all", null, selectall, toolbaritemorder.primary)); toolbaritems.add(new toolbaritem("none", null, selectnone, toolbaritemorder.primary)); } } private void handleimageselected(object sender, eventargs e) { if (onimageselected != null) { onimageselected(sender, e); } } void selectall() { foreach (var wi in wrappeditems) { wi.isselected = true; } } void selectnone() { foreach (var wi in wrappeditems) { wi.isselected = false; } } public static list<t> getselection() { return wrappeditems.where(item => item.isselected).select(wrappeditem => wrappeditem.item).tolist(); } } }
i removed backgroundcolorproperty
of grid , have added new column image of source property set based on row selected.
hope helps facing similar issue.
Comments
Post a Comment