Unselect WPF DataGrid when ItemsSource changed -
i have following datagrid in wpf :
<datagrid x:name="dgpatientmedicationorderlist" width="auto" horizontalalignment="stretch" rowheight="40" background="transparent" horizontalcontentalignment="left" gridlinesvisibility="none" rowheaderwidth="0" virtualizingstackpanel.virtualizationmode="standard" selectedindex="-1" scrollviewer.horizontalscrollbarvisibility="disabled" autogeneratecolumns="false" selectionmode="single" issynchronizedwithcurrentitem="true" rowdetailsvisibilitymode="visiblewhenselected" verticalalignment="stretch" isreadonly="true" itemssource="{binding patientorderscollectionview}"> when user clicks on row in datagrid, it's selectionchanged event fired , bound viewmodel command drives view load user control corresponding dg row. in view changing datagrid's source binding viewmodel. problem every time itemssource changed selectionchanged event fired selecting first item in datagrid; followed view loading user control without user explicitly selecting datagrid row. how can prevent datagrid selecting row when it's itemssource changed ?
simplified demo code:
xaml:
<button content="change source" command="{binding changeitemssourcecmd}" horizontalalignment="center" verticalalignment="bottom" margin="0,0,0,20" /> <stackpanel verticalalignment="bottom" horizontalalignment="left" orientation="vertical"> <textblock text="{binding selectedperson.id, stringformat=id: {0}}" /> <textblock text="{binding selectedperson.name, stringformat=name: {0}}" /> <textblock text="{binding selectedperson.gender, stringformat=gender: {0}}" /> <textblock text="{binding selectedperson.country, stringformat=country: {0}}" /> </stackpanel> </grid> viewmodel:
public class windowviewmodel : viewmodelbase { private person _selectedperson; private observablecollection<person> _personlist; public person selectedperson { { return _selectedperson; } set { raisepropertychange<person>(() => selectedperson, ref _selectedperson, ref value); } } public observablecollection<person> personlist { { return _personlist; } set { selectedperson = null; raisepropertychange<observablecollection<person>>(() => personlist, ref _personlist, ref value); } } public windowviewmodel() { personlist = new observablecollection<person>() { new person() { id=101, name="mahesh", gender="male", country="india"}, new person() { id=102, name="srinivas", gender="male", country="sri lanka"}, new person() { id=103, name="isha", gender="female", country="united states"}, new person() { id=104, name="salim", gender="male", country="pakistan"} }; } public icommand changeitemssourcecmd { { return new relaycommand(changeitemssourcecmdhandler); } } private void changeitemssourcecmdhandler() { personlist = new observablecollection<person>() { new person() { id=105, name="raman", gender="male", country="uganda"}, new person() { id=106, name="anurag", gender="male", country="england"}, new person() { id=107, name="komal", gender="female", country="thailand"}, new person() { id=108, name="nitin", gender="male", country="africa"} }; } }
you should:
1.add selecteditem binding in datagrid:
selecteditem="{binding selected, mode=twoway}" 2.have related property (firing propertychanged of course)
public object selected { { return selected; } set { selected = value; onpropertychanged("selected"); } } 3.set null in itemssource setter (or before change it)
public ienumerable patientorderscollectionview { { return patientorderscollectionview; } set { selected = null; // put null here unselect grid patientorderscollectionview = value; onpropertychanged("patientorderscollectionview"); } } should trick.
Comments
Post a Comment