views:

69

answers:

1

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?

A: 

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.

Peter