c# - Limit the Properties Bound to DataGrid in WPF using MVVM -
all, simple question. have mvvm application datagrid
have bound viewmodel using
<datagrid itemssource="{binding path=resources}" ...></datagrid>
where resources
defined via
public observablecollection<resourceviewmodel> resources { get; private set; }
in resourceviewmodel
class however, not have properties want appear in datagrid
, other properties do not want appear in datagrid
. resourceviewmodel
class
public class resourceviewmodel : workspaceviewmodel, idataerrorinfo { readonly resource resource; readonly resourcedatarepository resourcerepository; private bool isselected; public resourceviewmodel(resource resource, resourcedatarepository resourcerepository) { if (resource == null) throw new argumentnullexception("resource"); if (resourcerepository == null) throw new argumentnullexception("resourcerepository"); this.resource = resource; this.resourcerepository = resourcerepository; } public string keyindex { { return this.resource.keyindex; } set { if (value == this.resource.keyindex) return; this.resource.keyindex = value; base.onpropertychanged("keyindex"); } } public string filename { { return this.resource.filename; } set { if (value == this.resource.filename) return; this.resource.filename = value; base.onpropertychanged("filename"); } } public list<string> resourcestringlist { { return this.resource.resourcestringlist; } set { if (utilities.utilities.scrambledequals<string>(this.resource.resourcestringlist, value)) return; this.resource.resourcestringlist = value; base.onpropertychanged("resourcestringlist"); } } public bool isselected { { return isselected; } set { if (value == isselected) return; isselected = value; base.onpropertychanged("isselected"); } } }
where don't want isselected
appear in datagrid
, want each item in resourcestringlist
appear in different column of datagrid
. questions are:
1. how prevent isselected
showing [as checkbox
] in datagrid
?
2. how binding datagrid
automatically display items in separate columns?
what have tried:
i have attempted inherit
resourceviewmodel
class , bind instead, disgusting , more elegant solution; please :].i have no idea how proceed one. number of items stored in
list
variable , set @ runtime - needslist
.
as always, time.
i think options turn off auto generation silvermind mentioned (i.e. setting datagrid.autogeneratecolumns false , defining columns) or implement itypedlist. example create derived observablecollection implements itypedlist , returns properties based on attribute put on properties want hide.
public partial class mainwindow : window { public mainwindow() { this.datacontext = new typedlistobservablecollection<foo>(); initializecomponent(); } } public class typedlistobservablecollection<t> : observablecollection<t> , itypedlist { public typedlistobservablecollection() { } propertydescriptorcollection itypedlist.getitemproperties(propertydescriptor[] listaccessors) { return typedescriptor.getproperties(typeof(t), new attribute[] { browsableattribute.yes }); } string itypedlist.getlistname(propertydescriptor[] listaccessors) { return typeof(t).name; } } public class foo { public string name { get; set; } [browsable(false)] public bool isselected { get; set; } }
Comments
Post a Comment