1

I run my application and fill datagrid with data. Then I click on some row and handle event in following way:

  private void dataGridCanTabParamList_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        var buffer = sender as DataGrid;

        if ((buffer == null) || (buffer.CurrentColumn == null) )
            return;

        SetCanPropertyDesription(buffer.CurrentColumn.Header.ToString());

    }

When I run this event first time the CurrentColumn is null, when I run this event second time clicking in exactly the same position CurrentColumn contains data. CurrentItem is also empty when clicked first time.

Why I don't see the data at first click?

h__
  • 761
  • 3
  • 12
  • 41

2 Answers2

4

PreviewMouseDownEvent is tunneling event which is raised before actual MouseDown event.

And MouseDown event(handled by DataGridCell) is responsible for the selection of column in dataGrid. So, at first time your no cell is selected hence CurrentItem and CurrentColumn is null at that time.

See the propagation of events that how it works -

enter image description here

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
1

That is because CurrentColumn and CurrentItem refer to selected Column/Item. When you first click, nothing is selected as you handle the tunneled event (so your code is executed before the DataGrid actually set the current item) . When you click for the second time, CurrentColumn and CurrentItem have already been set.

Sisyphe
  • 4,626
  • 1
  • 25
  • 39
  • Is there other good event to use to access of current used column? Earlier I was using onSelectionChanged - but... it apears only on selection changed. – h__ Nov 15 '12 at 14:03
  • You can use the Bubbled event : MouseDown. But I'm almost certain this event will be already handled, so you will have to attach your handler in code behind using `UIElement.AddHandler` Method, specifying you want to handle events that are already handled. There may be a most suitable event though. – Sisyphe Nov 15 '12 at 14:07