c# - WPF DataGrid: LostKeyboardFocus vs. IsEditing and AddRows -


i have wpf datagrid single datagridtextcolumn. lets user enter variable number of dates (the dates wrapped in class, binding purposes).

but (using updatesourcetrigger=default) when user uses tab key navigate away datagrid, cell editing doesn't seem leave editing mode / validated. entered value shown in red, , row header shows exclamation mark framed dotted line.

setting updatesourcetrigger lostfocus fixes - when code resets underlying collection (to facilitate consecutive user input) single-item collection, datagrid shows single row. despite canuseraddrows="true" new, empty row goes missing (if code resets collection clearing it, column header shown).

i tried work around setting mode onewaytosource - datagrid no longer shows entered values.

if set updatesourcetrigger propertychanged, validation happens soon, resulting in bad user experience (dates should formatted dd/mm/yyyy. but, user enters "1/1", "01/01/2016" shown. using backspace edit year, causes value change "01/01/0201", , on)

there seems solution (https://social.msdn.microsoft.com/forums/vstudio/en-us/983b3b5b-7115-4821-b621-85b43578636e/how-can-i-end-a-datagrid-cell-edit-when-focus-switches-to-a-winforms-hosted-control?forum=wpf) that requires code-behind, i'd prefer cleaner solution.

my code:

<datagrid autogeneratecolumns="false"canuseraddrows="true" itemssource="{binding coursedatesparameterforrequest}"> <datagrid.columns>     <datagridtextcolumn header="datum" binding="{binding date, converterculture=nl-be, mode=twoway,          stringformat=\{0:dd/mm/yyyy\},  updatesourcetrigger=default, validatesondataerrors=true}">     </datagridtextcolumn> </datagrid.columns> </datagrid> 

private observablecollection<datewrapper> _coursedatesparameterforrequest; public observablecollection<datewrapper> coursedatesparameterforrequest {     { return _coursedatesparameterforrequest; }     set     {         if (value != _coursedatesparameterforrequest)         {             _coursedatesparameterforrequest = value;              if (propertychanged != null)             {                 propertychanged(this, new propertychangedeventargs(nameof(coursedatesparameterforrequest)));             }         }     } } 

   public class datewrapper     {         public int id { get; set; }         public datetime date { get; set; }         public datewrapper(datetime date)         {             date = date;         }         public datewrapper(){}     } 

thanks in advance!


Comments