c# - How to select a row in datagrid from ViewModel in MVVM? -


i'm trying validate user input in datagrid, way i'm doing like:

i) allow user add row

ii) allow user fill cells

iii)when endedit() event happens, update model

iv) if model not updated (for example when user didn't supply notnull values), error raised.

v) here tricky part! want stay on faulty row of datagrid, high lightened row change despite fact i've set selectedindex , selectedvalue correctly.

here xaml:

<datagrid x:name="gd_contacts" selecteditem="{binding selectedcontact,converter={staticresource newplaceconverter}}"                selectedindex="{binding selecteditemindex}"               margin="0,5,1,0" itemssource="{binding contactcollection, mode=twoway}"                canuseraddrows="true" canuserdeleterows="false" autogeneratecolumns="false" grid.column="1"> 

here viewmodel:

private observablecollection<actionenabledcontacts> fcontactcollection; //.... public actionenabledcontacts selectedcontact //the property selecteditem     {         { return fselectedcontact; }         set          {              //             list <actionenabledcontacts> invalidlist=contactcollection.where<actionenabledcontacts>(p => p.cnt_key == 0).tolist();             if (invalidlist != null && invalidlist.count > 0)             {                 //now find index of bastard                 list<actionenabledcontacts> allcontacts = contactcollection.tolist();                 int bastardindex = 0;                 foreach (actionenabledcontacts acontact in allcontacts)                 {                     if (acontact.cnt_key == 0)                     {                         fselectedcontact = invalidlist[0];                         notifypropertychanged();                         selecteditemindex = bastardindex;                         //^^^^ line update selectedindex , call notifypropertychanged()                     }                     bastardindex++;                 }             }             else             {                 //                 fselectedcontact = value;                 notifypropertychanged();             }         }     } 

you can't change selection while selection changing... if makes sense :p

if selection has changed, you'll have wait until can change programmatically once again. done using dispatcher.begininvoke add call dispatcher queue, executes after current selection processed.

if (acontact.cnt_key == 0) {     fselectedcontact = invalidlist[0];     notifypropertychanged();      application.current.dispatcher.begininvoke(new action(() =>         selecteditemindex = bastardindex)); } bastardindex++; 

doing in viewmodel not best solution, though - mvvm-wise or in multiple ui threads scenarios. raise event in line (passing index inside eventargs), have view subscribe event, , fire dispatcher call event handler (using this.dispatcher instead of application.current.dispatcher).


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -