简体   繁体   English

第一次调用时,Preview Mouse Down事件将返回引用了null的CurrentColumn的发件人

[英]Preview Mouse Down event returns sender with null referenced CurrentColumn when called first time

I run my application and fill datagrid with data. 我运行我的应用程序,并用数据填充datagrid。 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. 当我第一次运行此事件时,CurrentColumn为null,当我第二次运行此事件时,单击完全相同的位置CurrentColumn包含数据。 CurrentItem is also empty when clicked first time. 第一次单击时CurrentItem也为空。

Why I don't see the data at first click? 为什么第一次点击时看不到数据?

PreviewMouseDownEvent is tunneling event which is raised before actual MouseDown event. PreviewMouseDownEvent是隧道事件,在实际的MouseDown事件之前引发。

And MouseDown event(handled by DataGridCell) is responsible for the selection of column in dataGrid. MouseDown event(handled by DataGridCell)负责选择dataGrid中的列。 So, at first time your no cell is selected hence CurrentItem and CurrentColumn is null at that time. 因此,第一次没有选择任何单元格,因此CurrentItemCurrentColumn在那时为null。

See the propagation of events that how it works - 查看事件如何传播-

在此处输入图片说明

That is because CurrentColumn and CurrentItem refer to selected Column/Item. 这是因为CurrentColumn和CurrentItem引用选定的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) . 第一次单击时,在处理隧道事件时未选择任何内容(因此在DataGrid实际设置当前项之前执行您的代码)。 When you click for the second time, CurrentColumn and CurrentItem have already been set. 当您第二次单击时,已经设置了CurrentColumn和CurrentItem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM