c# - Applying a filter in ViewModel when value from the ViewModel change -
i have bind twice data on application. have 2 list view. first 1 list of element present checkbox. second list view have value checkbox checked.
so tried deal viewmode , 2 observablecollection. 1 full , 1 filtered. dont work. way ?
thank you!
here xaml code :
<!-- full collection --> <listview grid.row="1" grid.column="0" itemssource="{binding cycles.mydata}"> <listview.view> <gridview allowscolumnreorder="false"> <gridviewcolumn> <gridviewcolumn.celltemplate> <datatemplate> <checkbox style="{dynamicresource switchonly}" ischecked="{binding cycles.used, updatesourcetrigger=propertychanged}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn> <gridviewcolumn.celltemplate> <datatemplate> <textblock text="{binding name}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> </gridview> </listview.view> <!-- filtered collection --> <listview grid.column="2" grid.row="0" grid.rowspan="2" itemssource="{binding cycles.mydatafiltered}"> <listview.view> <gridview allowscolumnreorder="false"> <gridviewcolumn> <gridviewcolumn.celltemplate> <datatemplate> <checkbox style="{dynamicresource visualonly}" ischecked="{binding check}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn> <gridviewcolumn.celltemplate> <datatemplate> <textblock text="{binding name}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> </gridview> </listview.view> </listview>
here viewmodel :
public class viewmodel : propertychangedbase { public observablecollection<cycles> mydata { get; set; } public observablecollection<cycles> mydatafiltered { get; set; } private bool _used = true; public bool used { => _used; set { _used = value; _mydataview.filter = filter; onpropertychanged("used"); } } private cycles _currentselectedcycle; public cycles currentselectedfamily { => _currentselectedcycle; private set { _currentselectedcycle = value; onpropertychanged("currentselectedfamily"); } } readonly icollectionview _mydataview; public viewmodel() { mydata = new observablecollection<cycles>(); mydatafiltered = new observablecollection<cycles>(); _mydataview = collectionviewsource.getdefaultview(mydatafiltered); _mydataview.currentchanged += delegate { currentselectedfamily = (cycles)_mydataview.currentitem; }; } private static bool filter(object item) { var value = (cycles)item; return value != null && value.check; } }
here cycle class :
public class cycles : propertychangedbase { internal cycles() { _check = false; _firstday = 1; } private bool _check; private string _name; private int _firstday; private int _lastday; public bool check { => _check; set { if (_check == value) return; _check = value; onpropertychanged("check"); } } public string name { => _name; set { if (_name == value) return; _name = value; onpropertychanged("name"); } } public int firstday { => _firstday; set { if (_firstday == value) return; _firstday = value; onpropertychanged("firstday") } } public int lastday { => _lastday; set { if (_lastday == value) return; _lastday = value; onpropertychanged("lastday"); } } } public class propertychangedbase : inotifypropertychanged { public event propertychangedeventhandler propertychanged; protected void onpropertychanged(string propertyname) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
use below binding second list
itemssource="{binding selectedcycles}"
and in view model
public ienumerable<cycles> selectedcycles { { return mydata.where(x => x.check== true); } }
Comments
Post a Comment