Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable resizing of a column in a ListView

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.

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:

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?

like image 281
ygoe Avatar asked Feb 20 '23 01:02

ygoe


2 Answers

Tested:

<GridViewColumnHeader Content="Value" IsHitTestVisible="False"/>
like image 180
paparazzo Avatar answered Feb 21 '23 14:02

paparazzo


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.

Here is the code part from the 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:

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.

I really hope that this can help.

like image 40
Csendi Avatar answered Feb 21 '23 13:02

Csendi