wpf - c# - 'an unhandled exception of type 'System.NullReferenceException' -
this question has answer here:
- what nullreferenceexception, , how fix it? 32 answers
i'm newbie in mvvm, have model.cs contains few property of 'id' , 'positionname' , got view.cs contains datagrid selecteditems={binding items} , itemsource={binding position} , button command={binding showedits} after clicking encountered nullreference error @ 'items.positionname == null '.
here's code.
viewmodel.cs
class positionvm : inotifypropertychanged { private observablecollection<positionmodel> _position; private positionmodel _items; private icommand _showedits; public observablecollection<positionmodel> position { { return _position; } set { _position = value; notifyproperty("position"); } } public positionmodel items { { return _items; } set { _items = value; notifyproperty("items"); } } public icommand showedits { { if (_showedits == null) _showedits = new showedit(); return _showedits; } set { _showedits = value; } } public positionvm() { position = new observablecollection<positionmodel>(); position.add(new positionmodel() { id = 1, positionname = "asd" }); } public void showeditdialog() { if (items.positionname == null) { messagebox.show("error"); } else { positionview view = new positionview(); data.id = view.txtid.text; var z = new positionview(); z.showdialog(); } } public event propertychangedeventhandler propertychanged; private void notifyproperty(string info) { if(propertychanged != null) { propertychanged(this, new propertychangedeventargs(info)); } } }
why getting error? , how can avoid it? thanksss
i think problem icommand. try property:
public icommand showeditscommand { get; set; };
and of course need create it. use command class it, can use on first time, it's pretty simple:
/// <summary> /// relay implementation of icommand. /// </summary> public class relaycommand : icommand { private action execute; private predicate<object> canexecute; private event eventhandler canexecutechangedinternal; public relaycommand(action execute) : this(execute, defaultcanexecute) { } public relaycommand(action execute, predicate<object> canexecute) { if (execute == null) { throw new argumentnullexception("execute"); } if (canexecute == null) { throw new argumentnullexception("canexecute"); } this.execute = execute; this.canexecute = canexecute; } public event eventhandler canexecutechanged { add { commandmanager.requerysuggested += value; this.canexecutechangedinternal += value; } remove { commandmanager.requerysuggested -= value; this.canexecutechangedinternal -= value; } } public bool canexecute(object parameter) { return this.canexecute != null && this.canexecute(parameter); } public void execute(object parameter) { this.execute(); } public void oncanexecutechanged() { eventhandler handler = this.canexecutechangedinternal; if (handler != null) { handler.invoke(this, eventargs.empty); } } public void destroy() { this.canexecute = _ => false; this.execute = () => { return; }; } private static bool defaultcanexecute(object parameter) { return true; } }
after need initialize in viewmodel constructor , bind method:
showeditscommand = new relaycommand(showedits);
where showedits methods need run when command call:
public void showedits() { // here }
Comments
Post a Comment