i have a lis开发者_开发问答tview 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....
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;
}
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.
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
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.
精彩评论