how to fix the column width of a listview in c# windows form?

后端 未结 4 767
野性不改
野性不改 2021-01-13 20:43

i have a listview i need to fix the column width of the listview so that at run time user cannot drag the columnheaders and resize it.....what is the procedure?? i have sear

相关标签:
4条回答
  • 2021-01-13 20:49

    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.

    0 讨论(0)
  • 2021-01-13 20:52

    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;
    }
    
    0 讨论(0)
  • 2021-01-13 21:01

    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.

    0 讨论(0)
  • 2021-01-13 21:10

    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
    
    0 讨论(0)
提交回复
热议问题