views:

25

answers:

1

For a WPF ListView bound to an simple list that has no need or support for sorting, how you do make it so that the user doesn't get button press feedback when he clicks on a header? By default, clicking the middle of a column header feels like a button press for which nothing happens.

An example of the look and feel I'm trying to achieve is in System Control Panel > Advanced system settings > User Profile Settings. The Profile list does not support sorting. Accordingly, the header does not respond when clicked (except for column resize clicks).

+1  A: 

You could accomplish this by setting 'IsEnabled' to false on the columns headers that you do not want clickable. I've pasted an example that I used as a test. The only other thing I did was change the foreground brush so that the disabled column header appeared in black like the other header.

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="120">
                <GridViewColumnHeader IsEnabled="True"  Content="Col A" Foreground="Black"/>
            </GridViewColumn>
            <GridViewColumn Width="120">
                <GridViewColumnHeader IsEnabled="False" Content="Col B" Foreground="Black"/>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListViewItem>1</ListViewItem>
    <ListViewItem>4</ListViewItem>
    <ListViewItem>2</ListViewItem>
    <ListViewItem>3</ListViewItem>
</ListView>

The first column is clickable, the second column is not. Hope this helps!

edit: Sample referenced in my comment. This method leaves the header enabled but still doesn't allow it to be clicked:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="120">
                <GridViewColumnHeader Content="Col A"/>
            </GridViewColumn>
            <GridViewColumn Width="120">
                <GridViewColumnHeader Content="Col B" PreviewMouseDown="GridViewColumnHeader_PreviewMouseDown"/>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListViewItem>1</ListViewItem>
    <ListViewItem>4</ListViewItem>
    <ListViewItem>2</ListViewItem>
    <ListViewItem>3</ListViewItem>

And the code-behind for the event:

private void GridViewColumnHeader_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}

This basically 'eats' any clicks made on this button. Unfortunately it also stops you from resizing, since you need to click to resize. There may be a way to test the click to see whether it is a 'button' click or a 'resize area' click and only handle the button clicks, but I am not sure of it.

Ben Collier
When the column header is disabled, resizing is disabled along with it. Is there a way to keep resizing but disable the button press?
Edward Brey
Hmm, I tried using another method, which was to handle the 'PreviewMouseDown' event on the control. This allowed us to keep the header enabled but still not 'clickable', but it also prevented us from resizing. I think there must be a way to handle the 'PreviewMouseDown' event and allow a resize but not a button click, but I'm not sure how yet.
Ben Collier