Prevent user from resizing columns with WPF ListView

人盡茶涼 提交于 2019-12-28 06:18:34

问题


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


回答1:


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:


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>



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/181956/prevent-user-from-resizing-columns-with-wpf-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!