防止用户使用 WPF ListView 调整列大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/181956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:41:42  来源:igfitidea点击:

Prevent user from resizing columns with WPF ListView

wpfgridviewlistview

提问by Joachim Kerschbaumer

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

如何防止用户使用 ListView 控件调整 GridViewColumns 的大小?

采纳答案by Joachim Kerschbaumer

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_HeaderGripperfrom the template in order to prevent resizing of your columns.

您必须覆盖 GridViewColumnHeader 的 ControlTemplate(此处为默认模板)并从模板中删除PART_HeaderGripper以防止调整列的大小。

there is another solution that comes up with subclassing GridViewColumn described here. for representation purposes i prefer xaml only solutions though

还有另一种解决方案,它提出了此处描述的子类化 GridViewColumn 。出于表示目的,我更喜欢 xaml only 解决方案

回答by Jonathan Alfaro

For those looking for a quicker and simpler answer.

对于那些寻求更快更简单答案的人。

Set IsEnabled to False in the ColumnHeaderContainerStyle. This will prevent the user from resizing.

在 ColumnHeaderContainerStyle 中将 IsEnabled 设置为 False。这将阻止用户调整大小。

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.

如果您想修复禁用的灰色,请在 IsEnabled 属性上添加触发器并修复您需要的内容。

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

希望这可以帮助某人。

回答by InTheZone

Darkonekt's answer is good, however it may be preferable to set IsHitTestVisibleto false instead of IsEnabled. This has the benefit of not greying out the headers.

Darkonekt 的回答很好,但是最好设置IsHitTestVisible为 false 而不是IsEnabled。这样做的好处是不会使标题变灰。

<GridView.ColumnHeaderContainerStyle>
    <Style BasedOn="{StaticResource {x:Type GridViewColumnHeader}}" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="IsHitTestVisible" Value="False"/>
    </Style>
</GridView.ColumnHeaderContainerStyle>

回答by jmlumpkin

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

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.

我无法使用完整的 XAML 解决方案,因为由于它的动态性,我正在我的代码中构建所有内容。第一次尝试效果很好。