DataGrid ColumnHeader PreviewLeftMouseButtonDown事件限制

本文关键字:事件 PreviewLeftMouseButtonDown ColumnHeader DataGrid | 更新日期: 2023-09-27 18:19:17

我有这样的数据网格列标题:

<datagridtemplatecolumn.headertemplate>
    <datatemplate previewmouseleftbuttondown="TextBlock_DocumentsClick">
        <stackpanel orientation="Vertical">
            <textblock text="Doctor Name" previewmouseleftbuttondown="TextBlock_ReceptionsClick" name="DoctorName" />
            <textbox name="SearchReceptionDoctor" width="120" tag="DoctorName" text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.RecDoctorNameFilter, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                <textbox.inputbindings>
                    <keybinding key="Enter" command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.FilterReceptionsCommand}" commandparameter="{Binding Tag, ElementName=SearchReceptionDoctor}" />
                </textbox.inputbindings>
            </textbox>
        </stackpanel>
    </datatemplate>
</datagridtemplatecolumn.headertemplate>

现在它工作得很好,唯一的问题是,我想限制事件触发当我点击一个文本框。是否有一种方法来检查我是否点击了一个文本框,并防止我的事件的功能被解雇?这是我的事件在代码后面的样子:

private void TextBlock_BookingsClick(object sender, MouseButtonEventArgs e)
{
    try
    {
        (this.DataContext as PatientViewModel).ColumnName = (sender as TextBlock).Name.ToString();
        (this.DataContext as PatientViewModel).PatientTabBookingsSorting();
    }
    catch (Exception ex)
    {
        PanLogger.Log(LogLevel.Error, String.Format("Bookings tab datagrid text block click event failed! 'nReason: {0} 'nStackTrace: {1} 'nInnerException", ex.Message, ex.StackTrace, ex.InnerException));
    }
}

DataGrid ColumnHeader PreviewLeftMouseButtonDown事件限制

尝试MouseButtonEventArgs的OriginalSource属性,这将为您提供事件发生的确切控制

我将获取发送者的类型并检查这是否是一个文本框