Disallow button press feedback in non-sortable WPF ListView header
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).
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.
If you want to disable the header buttons for all ListViews, create a style in your Application.xaml file under Application.Resources. The headers will not be sizable when you do this.
<Application>
...
<Application.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<Border BorderBrush="LightGray" BorderThickness="1,0,1,0">
<TextBlock Text="{TemplateBinding Content}" TextAlignment="Center"
Background="LightSteelBlue" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
from... http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/02212923-85bc-4a20-a41c-c1b558fce8c1/
精彩评论