c# - WPF Datagrid single click row to open new Page -
my first post here! , new coding!...
i have wpf datagrid in frame on page. click (preferably single click) on row , use id value stored in 1 of columns navigate (open) new page.
using mousedoubleclick can double click row open page. throwing: "an unhandled exception of type 'system.nullreferenceexception' occurred in program.exe additional information: object reference not set instance of object."
on line (see code behind below complete method):
string id = ((datarowview)persondatagrid.selecteditem).row["personid"].tostring();
xaml:
<datagrid x:name="persondatagrid" autogeneratecolumns="false" selectionmode="single" selectionunit ="fullrow" mousedoubleclick="persondatagrid_cellclicked" > <datagrid.columns> <datagridtextcolumn binding="{binding path=personid}" clipboardcontentbinding="{x:null}" header="id" /> </datagrid.columns> <datagrid.cellstyle> <style targettype="datagridcell" basedon="{staticresource mydatagridcellstyle}"> <eventsetter event="datagridcell.mouseleftbuttondown" handler="persondatagrid_cellclicked"/> </style> </datagrid.cellstyle> </datagrid>
code behind:
private void persondatagrid_cellclicked(object sender, mousebuttoneventargs e) { string id = ((datarowview)persondatagrid.selecteditem).row["personid"].tostring(); selectedpersonid = int32.parse(id); this.navigationservice.navigate(new personprofile()); }
is there better way open personprofile page? there easy way open page using single click on row?
thanks.
a better way it, define collection of person
define datagrid
itemsource
, property of type person
contains selected item in datagrid
:
<datagrid x:name="persondatagrid" autogeneratecolumns="false" selectionmode="single" selectionunit ="fullrow" mouserightbuttonup="persondatagrid_cellclicked" itemssource="{binding persons}" selecteditem="{binding selectedperson}"> <datagrid.columns> <datagridtextcolumn binding="{binding path=personid}" header="id" /> <datagridtextcolumn binding="{binding path=personname}" header="name" /> </datagrid.columns> </datagrid>
and selectedperson
, persons
defined in code behind of page :
public partial class page1 : page,inotifypropertychanged { private observablecollection<person> _persons ; public observablecollection<person> persons { { return _persons; } set { if (_persons == value) { return; } _persons = value; onpropertychanged(); } } private person _selectedperson ; public person selectedperson { { return _selectedperson; } set { if (_selectedperson == value) { return; } _selectedperson = value; onpropertychanged(); } } public page1() { initializecomponent(); } public event propertychangedeventhandler propertychanged; [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } } public class person { public int personid { get; set; } public string personname { get; set; } }
the inotifypropertychanged
used notify ui of changes in properties. use selectedperson
property or pass new page, when receive mouserightbuttonup
event example in datagrid
:
private void persondatagrid_cellclicked(object sender, mousebuttoneventargs e) { if (selectedperson == null) return; this.navigationservice.navigate(new personprofile(selectedperson)); }
and change personprofile
page receive person in constructor.
Comments
Post a Comment