wpf 禁用调整 ListView 中列的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12605547/
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-13 05:33:27  来源:igfitidea点击:

Disable resizing of a column in a ListView

wpflistviewresize

提问by ygoe

There's a ListView in my window that has a number of columns. Some or all of the columns must not be resizable by the user because they already have the optimal width and accidental resizing only makes it worse. Now there's some answers to that question available, but all of them end up with restyling the control. Unfortunately then I end up with numerous pages of XAML code which is highly platform/theme-specific. When I create a copy of the default style with Blend, I get lots of gradients etc that only work on Win7 Aero, but not in XP theme or whatever will come.

我的窗口中有一个 ListView,它有许多列。用户不得调整部分或全部列的大小,因为它们已经具有最佳宽度,而意外调整大小只会使情况变得更糟。现在这个问题有一些可用的答案,但所有这些都以重新设计控件而告终。不幸的是,我最终得到了大量 XAML 代码页,这些代码页高度特定于平台/主题。当我使用 Blend 创建默认样式的副本时,我得到了许多仅适用于 Win7 Aero 的渐变等,但不适用于 XP 主题或即将出现的任何内容。

So replacing the entire style of a control is not an option. (It hardly ever really is.)

因此,替换控件的整个样式不是一种选择。(它几乎从来都不是。)

I've already identified the part that needs to be hidden, it's named "PART_HeaderGripper". I've done such things before, removing the running glow and other parts from a ProgressBar with the following code in code-behind:

我已经确定了需要隐藏的部分,它被命名为“PART_HeaderGripper”。我以前做过这样的事情,使用代码隐藏中的以下代码从 ProgressBar 中删除运行的发光和其他部分:

var glow = progressBar.Template.FindName("PART_GlowRect", progressBar) as FrameworkElement;
if (glow != null) glow.Visibility = visibility;

But this doesn't work with a GridViewColumnHeader because Template.FindName doesn't find anything (returns null). I'm pretty sure there must be a way to modify the visuals at runtime. But I can't figure it out right now. Any idea?

但这不适用于 GridViewColumnHeader,因为 Template.FindName 找不到任何东西(返回 null)。我很确定必须有一种方法可以在运行时修改视觉效果。但我现在想不通。任何的想法?

回答by paparazzo

Tested:

测试:

<GridViewColumnHeader Content="Value" IsHitTestVisible="False"/>

回答by Csendi

I had the same issue in one of my projects. I have found a solution, which I think not the best way, but at least works.

我在我的一个项目中遇到了同样的问题。我找到了一个解决方案,我认为这不是最好的方法,但至少有效。

I am using some databingind and click events to sort the items, so setting the column to readonly was not a way to go.

我正在使用一些 databingind 和 click 事件来对项目进行排序,因此将列设置为只读不是一种方法。

Here is the code part from the xml:

这是xml中的代码部分:

<ListView Name="ListWievResults" HorizontalAlignment="Left" Height="526" Margin="10,10,0,0" VerticalAlignment="Top" Width="400" ItemsSource="{Binding Results}" SelectedItem="{Binding SelectedResult}" IsSynchronizedWithCurrentItem="True" SelectionChanged="ListWievResults_SelectionChanged" SelectionMode="Single">
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridView.Columns>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Width="200">
                        <GridViewColumnHeader Content="Parameter" Click="GridViewColumnHeader_Click" PreviewMouseMove="GridViewColumnHeader_PreviewMouseMove"/>
                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Width="100">
                        <GridViewColumnHeader Content="SAFE ID" Click="GridViewColumnHeader_Click" PreviewMouseMove="GridViewColumnHeader_PreviewMouseMove"/>
                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Author}" Width="95">
                        <GridViewColumnHeader Content="Author" Click="GridViewColumnHeader_Click" PreviewMouseMove="GridViewColumnHeader_PreviewMouseMove"/>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

and the related part from th xaml.cs file:

以及来自 xaml.cs 文件的相关部分:

private void GridViewColumnHeader_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        e.Handled = true;
    }

This is not the most beautiful way as the marker will still be there showing that you can resize the column, but at least it does not allow it. One drawback is that if you want to use the "PreviewMouseMove" event for something else, that will not work.

这不是最漂亮的方式,因为标记仍然存在,表明您可以调整列的大小,但至少它不允许这样做。一个缺点是,如果您想将“PreviewMouseMove”事件用于其他用途,那将不起作用。

I really hope that this can help.

我真的希望这能有所帮助。