如何捕获“点击”? DataGrid列标题上的事件 [英] How do I capture "Click" events on a DataGrid column headers

查看:117
本文介绍了如何捕获“点击”? DataGrid列标题上的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 4附带的 DataGrid 似乎不包含列和行标题单击的事件。我想接收列标题单击的事件,因为我想提供自己的排序行为并关闭默认排序,这是因为我的视图模型是一个分页模型,一次只能显示25/50/100行,默认排序将仅对当前页面进行排序。



现在,我可以创建新的 DataGridColumnHeader 样式其中包含一个clickable元素并设置 ColumnHeaderStyle ,尽管这似乎很痛苦,因为我将很难弄清楚被单击的列等内容。 / p>

还有其他人遇到这个问题并解决了吗?

解决方案

标头只是按钮。像任何按钮一样,您可以注册 Click 事件以捕获这些点击。只需设置一个针对 DataGridColumnHeader 的样式,然后添加一个 Click 事件处理程序即可。然后在处理程序中,您可以直接通过发件人访问标头。然后,您可以获取与该标头关联的和与之关联的其他信息。

 < DataGrid> 
< DataGrid.Resources>
< Style TargetType = DataGridColumnHeader>
< EventSetter Event = Click Handler = columnHeader_Click />
< / Style>
< /DataGrid.Resources>
< / DataGrid>

然后在代码中:

  private void columnHeader_Click(对象发送者,RoutedEventArgs e)
{
var columnHeader =发送者为DataGridColumnHeader;
if(columnHeader!= null)
{
//做东西
}
}






进一步查看 DataGrid ,我注意到有一个 ColumnHeaderStyle 属性。我认为,最好通过此属性来应用样式。

 < DataGrid> 
< DataGrid.ColumnHeaderStyle>
< Style TargetType = DataGridColumnHeader>
< EventSetter Event = Click Handler = columnHeader_Click />
< / Style>
< /DataGrid.ColumnHeaderStyle>
< / DataGrid>


It appears that the DataGrid that comes with .NET 4 does not contain an event for both column and row header clicks. I want to receive events for the column header click as I want to provide my own sorting behaviour and turn off the default sorting, this is because my view-model is a paginated model that will only display 25/50/100 rows at a time, the default sorting will of-course sort the current page only.

Now I could create a new DataGridColumnHeader style which contains a clickable element and and set ColumnHeaderStyle, though this just seems like a pain as I'll have trouble figuring out things like which column it was that got clicked etc.

Anyone else come up against this and solved it?

解决方案

The headers are just buttons. Like any button, you can register to the Click event to capture those clicks. Just set a style targeting DataGridColumnHeader and add a Click event handler. Then within the handler, you have access to the header directly via the sender. You could then get the Column associated with that header and other information associated with it.

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridColumnHeader">
            <EventSetter Event="Click" Handler="columnHeader_Click" />
        </Style>
    </DataGrid.Resources>
</DataGrid>

Then in the code:

private void columnHeader_Click(object sender, RoutedEventArgs e)
{
    var columnHeader = sender as DataGridColumnHeader;
    if (columnHeader != null)
    {
        // do stuff
    }
}


Looking further into the DataGrid, I noticed that there's a ColumnHeaderStyle property. I think it would be a better idea to apply the style through this property instead.

<DataGrid>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <EventSetter Event="Click" Handler="columnHeader_Click" />
        </Style>
    </DataGrid.ColumnHeaderStyle>
</DataGrid>

这篇关于如何捕获“点击”? DataGrid列标题上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆