c# - wpf MVVM set constructor -
for example have window 2 columns. in first column want show items have property isfavorite = true
in second isfavorite = false
. when using simleioc container seems coudnot change parameter's in view. need create different viewmodel each view? or how can manipulate listviewmodel in way?
listviewmodel
namespace wpfapplication169.viewmodel { /// <summary> /// class contains properties view can data bind to. /// <para> /// see http://www.galasoft.ch/mvvm /// </para> /// </summary> public class listviewmodel : viewmodelbase { /// <summary> /// initializes new instance of listviewmodel class. /// </summary> /// public observablecollection<liststruct> items { get; set; } public listviewmodel() { items = new observablecollection<liststruct>(); items.add(new liststruct { carname = "toyota", isfavorite = true}); items.add(new liststruct { carname = "dongfeng", isfavorite = false }); } } public class liststruct { public bool isfavorite {get;set;} public string carname {get;set;} } }
mainviewmodel
namespace wpfapplication169.viewmodel { public class mainviewmodel : viewmodelbase { /// <summary> /// initializes new instance of mainviewmodel class. /// </summary> public mainviewmodel() { } } }
listviewxaml.cs:
<usercontrol x:class="wpfapplication169.listview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="300" d:designwidth="300" datacontext="{binding path=listviewmodel, source={staticresource locator}}"> <grid> <itemscontrol itemssource="{binding items}"> <itemscontrol.itemtemplate> <datatemplate> <label content="{binding carname}"></label> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid>
mainwindow.xaml.cs
<grid> <grid.rowdefinitions> <rowdefinition height="auto"></rowdefinition> <rowdefinition></rowdefinition> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition></columndefinition> <columndefinition></columndefinition> </grid.columndefinitions> <view:listview grid.column="0" grid.row="1"></view:listview> <view:listview grid.column="1" grid.row="1"></view:listview> <label grid.column="0" grid.row="0" content="isfavorite"></label> <label grid.column="1" grid.row="0" content="notfavorite"></label> </grid>
tell user control want favorite/notfavorite items. adding public property showfavorite our usercontrol.
we need filter our itemssource according showfavorite. applying filtering on original collection. our listviewmodel doesn't know happening.
public partial class listview : usercontrol { public listview() { this.initializecomponent(); } bool _showfavorite; public bool showfavorite{ get{return _showfavorite;} set{_showfavorite = value; if(value == true) { collectionview v = (collectionview)collectionviewsource.getdefaultview(myitemscontrol.itemssource); v.filter = (i)=>{return ((liststruct)i).isfavorite==true;}; v.refresh(); } else { collectionview v = (collectionview)collectionviewsource.getdefaultview(myitemscontrol.itemssource); v.filter = (i)=>{return ((liststruct)i).isfavorite==false;}; v.refresh(); } } }
}
i can't make code work, changed it.
<usercontrol x:class="wpfapp1.listview" ... xmlns:viewmodel="clr-namespace:wpfapp1" ... > <usercontrol.datacontext> <viewmodel:listviewmodel/> </usercontrol.datacontext> <grid x:name="mygrid"> <itemscontrol x:name="myitemscontrol" itemssource="{binding items}"> <itemscontrol.itemtemplate> <datatemplate> <label content="{binding carname}"></label> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid> </usercontrol>
mainviewmodel.cs
using system; using system.componentmodel; using system.collections; using system.collections.generic; using system.collections.objectmodel; namespace wpfapp1 { public class mainviewmodel { public listviewmodel mylistviewmodel{get;set;} public mainviewmodel() { mylistviewmodel = new listviewmodel(); } } public class listviewmodel { public observablecollection<liststruct> items { get; set; } public listviewmodel() { items = new observablecollection<liststruct>(); items.add(new liststruct { carname = "toyota", isfavorite = true}); items.add(new liststruct { carname = "dongfeng", isfavorite = false }); } } public class liststruct { public bool isfavorite {get;set;} public string carname {get;set;} } }
mainwindow.xaml
<view:listview x:name="favoritelistview" grid.column="0" grid.row="1" showfavorite="true"></view:listview> <view:listview x:name="notfavoritelistview" grid.column="1" grid.row="1" showfavorite="false"></view:listview> <label grid.column="0" grid.row="0" content="isfavorite"></label> <label grid.column="1" grid.row="0" content="notfavorite"></label>
Comments
Post a Comment