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

北慕城南 提交于 2019-12-10 10:53:16

问题


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


回答1:


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;
}



回答2:


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.




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/2460308/how-to-fix-the-column-width-of-a-listview-in-c-sharp-windows-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!