How can I prevent a user from resizing GridViewColumns withing a ListView control?
5 Answers
For those looking for a quicker and simpler answer.
Set IsEnabled to False in the ColumnHeaderContainerStyle. This will prevent the user from resizing.
Like this:
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
If you want to fix the disabled grayed out color add a trigger on the IsEnabled property and fix what you need.
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</GridView.ColumnHeaderContainerStyle>
This answer might not be as elegant as other posted; but in my case all I needed was a quick way of doing it.
Hope this helps someone.
-
2
-
6Both this and InTheZone's answer are good, but if you need the column headers to be clickable for sorting etc., they won't work.– Josh NoeJun 13, 2016 at 2:02
Darkonekt's answer is good, however it may be preferable to set IsHitTestVisible
to false instead of IsEnabled
. This has the benefit of not greying out the headers.
<GridView.ColumnHeaderContainerStyle>
<Style BasedOn="{StaticResource {x:Type GridViewColumnHeader}}" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
-
2
-
i found a solution and probably it will help someone else someday ;)
you have to override the GridViewColumnHeader's ControlTemplate (default template is here ) and remove the PART_HeaderGripper from the template in order to prevent resizing of your columns.
there is another solution that comes up with subclassing GridViewColumn described here. for representation purposes i prefer xaml only solutions though
-
2First solution is better for the usability point of view, since the user simply cannot change the width. With the second option, the user sees a cursor that suggests he can resize the column, but it doesn't resize. May 24, 2012 at 19:17
-
The problem of the first approach: there is no visual separator between adjacent columns. May 24, 2012 at 19:48
-
1The best solution I've found is to set IsEnabled=false for that Thumb. May 24, 2012 at 20:17
-
To use the first solution (overriding ControlTemplate), copy
GridViewColumnHeaderGripper
,GridViewColumnHeader
, and the colors into your Resources. Change the first line of theGridViewColumnHeader
style to be<Style x:Key="GridViewColumnHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
. Finally, apply it to your GridView by doing<ListView.View><GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle}">...
– PakmanNov 25, 2014 at 21:16 -
Also, it seems like the linked MS style is a little wonky. Aside from the
Key
andTargetType
being reversed in theGridViewColumnHeader
style, the header is colorized a gradient of purple.– PakmanNov 25, 2014 at 21:18
A smooth solution:
<GridViewColumn ...>
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
-
this disabled scrolling and does not work well with keybobard resizing.... also this is a duplicate answer.... @InTheZone already proposed the exact same code Jun 26, 2022 at 6:05
I was able to do something similar with the instructions in this post
http://blogs.msdn.com/b/atc_avalon_team/archive/2006/04/11/573037.aspx
I wasn't able to use a full XAML solution, since I was building everything in my code behind due to the dynamics of it. Worked great on the first try.