How to fix the column width of a listview in c# Windows Form?
Asked Answered
S

4

4

I have a listview, and I need to fix the column width of the listview so that at runtime user cannot drag the column headers and resize it... What is the procedure?? I have searched all the properties but none of them help me out to solve this pbm... This is possible in gridview but how will it be possible in listview?

Seduction answered 17/3, 2010 at 6:40 Comment(0)
B
16

The easiest way is to use ColumnWidthChanging event:

private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
    e.Cancel = true;
    e.NewWidth = listView.Columns[e.ColumnIndex].Width;
}
Brink answered 17/3, 2010 at 7:8 Comment(4)
This is yet another example of why using a ListView is so hard -- it's hard to get everything right. This code will not catch attempts resize columns via the keyboard, most notably Ctrl-Numpad-+. That will resize all your columns to minimum calculated width, even with this event handler in place. After they have been resized, the user won't be able to put them back again.Playpen
OMG, I didn't know about Ctrl-Numpad-+Brink
This won't work if "Show Window Contents While Dragging" is turned off globally in windows -- the event never happens.Hedden
This doesn't seem to work on Server 2008 R2, not exactly sure why though. Works perfect on Windows 7 and 8.Hemingway
P
1

Use ObjectListView. That not only allows individual columns to be fixed width, but to have minimum and maximum widths as well. It does the hard work of catching all cases, including Ctrl-Numpad-+, so that they cannot be circumvented.

Playpen answered 17/3, 2010 at 11:43 Comment(0)
P
0

Thanks a lot I've used it in vb.net as

 Private Sub ListView1_ColumnWidthChanging(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnWidthChangingEventArgs) Handles ListView1.ColumnWidthChanging
     e.Cancel = True
     e.NewWidth = ListView1.Columns(e.ColumnIndex).Width    
 End Sub
Particularity answered 1/8, 2010 at 7:36 Comment(0)
S
0

One way of achieving this is by setting the Selector.IsEnabled to false.

I'll put a code which I used in one of my applications that I was working on, it is simple you'll get it easily.

ListView code (Focus on GridView's ColumnHeaderContainerStyle property) -

<ListView Grid.Row="1" BorderBrush="{StaticResource MainForegroundBrush}" BorderThickness="1" 
                          HorizontalContentAlignment="Center" FontSize="11" Width="auto" Height="auto" 
                          ItemsSource="{Binding CurrentPkgs,UpdateSourceTrigger=PropertyChanged}" 
                          Style="{DynamicResource ListViewStyle1}" ItemContainerStyle="{DynamicResource ListViewItemStyle1}">
                    <ListView.View>

                        <GridView ScrollViewer.VerticalScrollBarVisibility="Visible" AllowsColumnReorder="False" 
                                  ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
                            <GridViewColumn Header="ManualId" Width="70" DisplayMemberBinding="{Binding Path=ManualId}" />
                            <GridViewColumn Header="ManualPath" Width="210" DisplayMemberBinding="{Binding Path=ManualPath}" />
                            <GridViewColumn Header="Revision" Width="60" DisplayMemberBinding="{Binding Path=RevVersion}" />
                            <GridViewColumn Header="PublishedOn"  Width="80" DisplayMemberBinding="{Binding Path=PublishedOn}" />
                            <GridViewColumn Header="PackageId" Width="70" DisplayMemberBinding="{Binding Path=PackageId}" />
                        </GridView>
                    </ListView.View>
                </ListView>

For myHeaderStyle (Focus on Selector.IsEnabled property and Trigger for IsEnabled) -

<Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="MinWidth" Value="50"/>
        <Setter Property="Selector.IsEnabled" Value="False"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Background" Value="{StaticResource MainBackgroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource MainForegroundBrush}"/>
        <Setter Property="BorderBrush" Value="#999"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="#111"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="#ccc"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Now you won't be able to resize the columns and they will look disabled as well. For that just add a trigger on property IsEnabled then it will look the way you want it to.

Settles answered 14/2, 2019 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.