Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from resizing columns with WPF ListView

How can I prevent a user from resizing GridViewColumns withing a ListView control?

like image 747
Joachim Kerschbaumer Avatar asked Oct 08 '08 09:10

Joachim Kerschbaumer


3 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.

like image 120
Jonathan Alfaro Avatar answered Oct 12 '22 04:10

Jonathan Alfaro


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>
like image 23
InTheZone Avatar answered Oct 12 '22 06:10

InTheZone


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

like image 29
Joachim Kerschbaumer Avatar answered Oct 12 '22 05:10

Joachim Kerschbaumer