c# - Datagrid loses keyboard focus when deleting an item -


i have found similar questions, none have solved problem, have put small example.

i want able press d key, , delete item observablecollection. works expected.

i want able continue maipulating datagrid using arrow keys , d key row after 1 deleted (i.e. index of updated datagrid equal index deleted item had).

the useful answer have found 1 - focus on datagridcell selecteditem when datagrid receives keyboard focus - i'm not sure when should calling want call after view has been updated, i'm using selectionchanged event being called far use.

any advice appreciated, hope have provided enough code below enable recreate project , replicate problem.

many thanks, mike

my xaml code:

<window x:class="datagridproblem.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525">     <grid>         <datagrid itemssource="{binding myitems}"                   selecteditem="{binding selecteditem}">             <datagrid.inputbindings>                 <keybinding key="d" command="{binding delete}"/>             </datagrid.inputbindings>         </datagrid>     </grid> </window> 

my view model:

namespace datagridproblem {     public class viewmodel : inotifypropertychanged     {         public event propertychangedeventhandler propertychanged;          private observablecollection<myitem> _myitems;         private icommand _delete;          public myitem selecteditem { get; set; }         public icommand delete { { return _delete; } }         public observablecollection<myitem> myitems         {             { return _myitems; }             set             {                 _myitems = value;                 onpropertychanged("myitems");             }         }          public viewmodel()         {             _myitems = new observablecollection<myitem>();             _myitems.add(new myitem() { name = "john" });             _myitems.add(new myitem() { name = "mike" });             _myitems.add(new myitem() { name = "phil" });              _delete = new relaycommand(deleteselected);         }          private void deleteselected(object obj)         {             myitems.remove(selecteditem);         }          private void onpropertychanged(string propertyname)         {             propertychangedeventhandler handler = propertychanged;             if (handler != null) handler(this, new propertychangedeventargs(propertyname));         }     } } 

myitem:

namespace datagridproblem {     public class myitem     {         public string name { get; set; }     } } 

i try this:

private void deleteselected(object obj)     {         var index = myitems.indexof(selecteditem);         myitems.remove(selecteditem);         if (myitems.count > 0)            selecteditem = myitems[index >= myitems.count ? index-1 : index];     } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -