This project is read-only.

Datagrid : discrepancy between 'datagrid.CurrentItem' & 'datagrid.Items.CurrentItem' when datagrid acquires focus



This test is a follow up on Samantha's information (thank you) about how to interpret correctly " item currency ".
Plse read the issue : Datagrid : 'datagrid.CurrentItem' information disappears when focus outside datagrid [wpf: 9992]
The annexed file demonstrates :
  • said discrepancy when the user tabs from outside the grid into the first cell of the grid (cell A).
  • the discrepancy disappears when the user tabs into the second colum aside cell A.


Unzip annexed file
Start Visual Studio 2008 and adjust the reference to 'WPFToolkit.dll' (I used v3.5.40128.1) in the solution project


Start Application

Step 1:

1.1. Tab once :
the 'Items.CurrentItem' textbox remains empty (strange) while the DOS window reports 'DataGrid.Items.CurrentItem: A' (correct)
the 'CurrentItem' textbox reports 'A' : correct
1.2. Tab once : the 'Items.CurrentItem' textbox now contains the correct information
conclusion : when tabbing into cell 'A', the information is not in sync in 'textbox : Items.CurrentItem'; otherwise it is

Step 2:

2.1. Tab all grid around, as well as the textboxes until cell A is framed again. The result is
the 'Items.CurrentItem' textbox and the DOS window report 'DataGrid.Items.CurrentItem: C'
the 'CurrentItem' textbox reports 'A'
2.2. Tab once to focus the cell next to A: all information is again correct
conclusion : when tabbing into cell 'A', the information is not in sync in both 'textbox : Items.CurrentItem' and MS-DOS console with respect to 'DataGrid.CurrentItem'; otherwise it is

file attachments


Julie wrote May 28, 2009 at 9:47 AM

Three additional comments :

1.- The concept of DataGrid.CurrentItem looks to me like a DataGrid.FocusedItem/Cell concept. Am I wrong ?

I produced a 'version 2' (enriches the original version) with MS-DOS console displaying 'Keyboard.FocusedElement' as well as 'FocusManager.GetFocusedElement'. The Datagrid now has attribute FocusManager.IsFocusScope="True". This will provide even better insight into the necessary synchronization of all this information.

2.- From a general WPF design standpoint, I find it regrettable that the concept 'CurrentItem' (FocusedItem) introduced by the DataGrid team is not elevated to the base class ItemsControl.

3.- The .Net 4 beta 1 WPF documentation states :
DataGrid.CurrentItem : Gets the data item bound to the row that contains the current cell.
DataGrid.CurrentCell : Gets or sets the cell that has focus.
This is consistent with Samantha's explanation in work item [wpf: 9992]. However, I would like to know whether we are talking here about Keyboard focus or FocusManager scope (logical) focus or both.

wrote May 28, 2009 at 9:47 AM

snovik wrote May 28, 2009 at 9:59 AM

And to move my comment from the old thread - how can I achieve the following:

CurrentCell="{Binding CurrentGridCell, Mode=OneWayToSource}

I currently subscribe to the event, but it breaks the whole MVVM pattern. It seems that binding is lost with focus. Moreover, it seems that binding is lost when ItemsSource is changed on the DataGrid

Julie wrote May 28, 2009 at 2:27 PM

My answer to Snovik:

1.- When dealing with MVVM, I have so far always referred to the CollectionView.CurrentItem value. I guess I did this out of habit with other ItemsControls like ListBox, etc. that do not have
a "ItemsControl.CurrentItem" property. Of course, I am then missing the "CurrentCell" aspect on the DataGrid since the CollectionView does not provide me with a "CurrentProperty" value.
But because of standard DataBinding notification aspects from datagrid cell to business object property, I have not come yet to require this "CurrentCell/Property" in my MVVM. Do you have a compelling case against this theory ?

2.- When dealing with Commands in MVVM, I rely on the standard RoutedUICommand system of WPF and that works perfectly for me. This system relies largely on the principle of
FocusedElement to "CanExecute" a command and I must say that works despite the many reported difficulties about this on the MSFT WPF forum.

3.- Comes in the DataGrid.CurrentItem. I am still in the process of understanding what it "really" means with respect to the above two concepts and how it deviates or not from them.
From my previous comments / questions, you see I still am quite puzzled and "for the time being" still not using this property in my projects.

Hope all this helps you,

Kind regards.

wrote Aug 21, 2009 at 7:15 PM

wrote Feb 7, 2010 at 11:15 AM

wrote Feb 22, 2010 at 10:24 PM

wrote Feb 22, 2013 at 2:41 AM