0

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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.