どのようにC#のWindowsフォームでリストビューの列幅を修正するには?
質問
私は、実行時にユーザーが手順が何であるか..... columnheadersをドラッグしてサイズを変更できないように??リストビューの列の幅を修正する必要があるリストビューを持っています 私はすべてのプロパティを検索しましたが、それらのどれもが、このPBMを解決するために私を助けていません。.. これは、GridViewの中で可能ですが、どのようにそれが可能になり、リストビューで....
解決
最も簡単な方法は、ColumnWidthChanging
イベントを使用することです。
private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
e.Cancel = true;
e.NewWidth = listView.Columns[e.ColumnIndex].Width;
}
他のヒント
ObjectListView して使用してください。すなわち、個々の列の幅を固定することを可能にするが、同様に最小値と最大幅を有するだけでなく。それは、彼らは回避することができないように、Ctrlキーを押しながらNumpad- +を含むのすべてのの例を、引くのハードワークを行います。
おかげで、私は
としてvb.netでそれを使用していた多くの 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
これを達成する一つの方法はfalseにSelector.IsEnabledを設定することである。
私は私が働いていた私のアプリケーションの一つで使用されるコードを入れます、あなたが簡単にそれを得ることができます簡単です。
ListViewのコード(GridViewのColumnHeaderContainerStyle特性に焦点を当てる) -
<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>
myHeaderStyle(でIsEnabledためSelector.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>
これは、列のサイズを変更することはできませんし、彼らは同様に無効になります。それはちょうど財産でIsEnabledにトリガーを追加するために、それはあなたがそれを望むようになります。
所属していません StackOverflow