Friday 15 June 2012

c# - Updating WPF Datagrid's SelectedItem from a ViewModel and have the changes reflected back in the Datagrid -



c# - Updating WPF Datagrid's SelectedItem from a ViewModel and have the changes reflected back in the Datagrid -

i've searched solution problem long time , it's driving me insane. don't seem find problem , in order prepare it.

on problem @ hand: i'm working on project in i'm using wpf in combination mvvm pattern. have datagrid have bound observablecollection property in viewmodel. have selecteditem property of datagrid bound currentstudent property in viewmodel.

now, want able update, in database, selected pupil in datagrid whenever alter his/her name, lastname, studentnumber or e-mail. can update pupil in database, can access selected pupil in datagrid via selectedstudent property in viewmodel. big problem want alter property in viewmodel straight , have changes selected pupil reflected datagrid create changes. can't, life of me, figure out doing wrong. @ first, need in order accomplish goal seems easy , trivial, doesn't work. can selected pupil viewmodel property can't create changes visible in datagrid unless close view , reload , have application load students database , see changes in datagrid.

here code doesn't work in viewmodel:

estudianteseleccionado.nombres = nombres; estudianteseleccionado.apellidos = apellidos; estudianteseleccionado.email = email; estudianteseleccionado.matricula = matricula; bc.actualizarestudiante(estudianteseleccionado);

this code in charge of updating selected student. here definition of property estudianteseleccionado:

private estudiante _estudianteseleccionado; public estudiante estudianteseleccionado { { homecoming _estudianteseleccionado; } set{ if(value != _estudianteseleccionado){ _estudianteseleccionado = value; onpropertychanged(); } } }

as said, property datagrids selecteditem bound in view. next xaml datagrid:

<datagrid itemssource="{binding estudiantes, updatesourcetrigger=propertychanged, mode=twoway}" selecteditem="{binding estudianteseleccionado, updatesourcetrigger=propertychanged, mode=twoway}" autogeneratecolumns="false"> <datagrid.columns> <datagridtextcolumn header="matrĂ­cula" isreadonly="true" binding="{binding matricula, updatesourcetrigger=propertychanged, mode=twoway}"/> <datagridtextcolumn header="nombres" isreadonly="true" binding="{binding nombres, updatesourcetrigger=propertychanged, mode=twoway}"/> <datagridtextcolumn header="apellidos" isreadonly="true" binding="{binding apellidos, updatesourcetrigger=propertychanged, mode=twoway}"/> <datagridtextcolumn header="email" isreadonly="true" binding="{binding email, updatesourcetrigger=propertychanged, mode=twoway}"/> </datagrid.columns> </datagrid>

one lastly note: checked see if collection of students updated 1 time modify selectedstudent property within viewmodel , change. doesn't seem notify datagrid item in collection has changed. here property represents students collection:

public observablecollection<estudiante> estudiantes { { homecoming _estudiantes; } set { if (_estudiantes != value) { _estudiantes = value; onpropertychanged(); } } }

i think there might problem bindings in datagrid's datagridtextcolumn. maybe not beingness notified of changes, have tried every setting , doesn't notified when item in collection gets modified viewmodel.

thanks user nnmait solve problem. model classes did not implement inotifypropertychanged, viewmodels. implementing interface in model classes solved this.

c# wpf xaml mvvm datagrid

No comments:

Post a Comment