我正在尝试实现以下目标
1. 将 gridview 列的最小宽度设置为某个值。
2. 当条目添加到该列时,列宽应相应调整。
我已经为它编写了以下代码
<Grid Background="white" Height="140" Width="auto">
<ListView Style="{StaticResource stl_lvi_TEXT}" Grid.Row="0" Grid.Column='0' Name="lv_includedInstruments" SelectionMode="Single" Background="White" Height="140" Width="auto" HorizontalAlignment ="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Coll_History}" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="lv_includedInstruments_SelectionChanged" TabIndex="18" GotFocus="lv_includedInstruments_GotFocus">
<ListView.View>
<GridView>
<GridViewColumn Width="212" >
<GridViewColumn.Header>
<GridViewColumnHeader Style="{StaticResource stl_ColumnHeaderStyle}" Content="Groups" HorizontalContentAlignment="Left" Background="white" FontSize="12" BorderBrush="#FF97BADA" BorderThickness="1" Tag="ProtocolVisitTypeForm.ProtocolName"/>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="auto" Tag="{Binding}" Text="{Binding Path=ProtocolVisitTypeForm.ProtocolName}" >
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
stl_ColumnHeaderStyle 如下
<Style x:Key="stl_ColumnHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock MinWidth="200" Width="auto" TextWrapping="Wrap" Text="{Binding}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
当我添加长度为 x 的字符串的第一个条目时,这工作正常。但如果后续条目的长度大于 x,则列宽不会扩大。
谢谢你。