WPF Listview Columnheader Click Event

泄露秘密 提交于 2019-12-11 16:26:35

问题


I have a ListView (GridView) in WPF and I'm trying to implement sorting according to http://msdn.microsoft.com/en-us/library/ms745786.aspx. In my case, the celltemplate for one of the columns contains an Expander. Now when I click the expander header, the GridViewColumnHeader.Click event fires. How do I prevent this from happening?


回答1:


If nothing needs to happen, cancel it with e.Cancel = true. I have something like that in a project of mine, where I don't want the user to reorder the columns:

private void DataGrid_ColumnReordering(object sender, Microsoft.Windows.Controls.DataGridColumnReorderingEventArgs e)
{
    e.Cancel = true;
}

Then, in the XAML, I have:

<toolkit:DataGrid ItemsSource="{Binding JournalItems}" 
                  AutoGenerateColumns="True"
                  ColumnReordering="DataGrid_ColumnReordering">

This is the WPF Toolkit datagrid, but the e.Cancel = true should work for any control.

If other things need to happen when the user clicks this header, you can also handle it in that method.

You could check the sender to see where the user clicked (on the expander or on the gridview header) if you need to handle these cases differently. If the sender is the expander, cancel it. If the sender is the gridview header, let the sorting continue.



来源:https://stackoverflow.com/questions/3716819/wpf-listview-columnheader-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!