有没有办法解决这个问题,还是我必须将 GridViewColumn 对齐到左侧或中心?例如,我有一列以 MB 为单位显示文件大小,并且我将其与右侧对齐,但是如果我更改列的宽度,则列中的文本会消失。
<ListView Name="myLV" SelectionMode="Single" ItemcontainerStyle="{DynamicResource lvis}" ItemSource="{Binding mySource}">
<ListView.View>
<GridView>
<GridViewColumn Header="Filename" local:MainWindow.SortName="Model.filename">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="0" Text="{Binding DisplayFilename}" VerticalAlignment="Center" Width="70" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Filesize" local:MainWindow.SortName="Model.FileSize">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="0" Text="{Binding DisplayFileSize}" VerticalAlignment="Center" TextAlignment="Right" Width="123" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我的解决方案:
<ListView Name="myLV" SelectionMode="Single" ItemcontainerStyle="{DynamicResource lvis}" ItemSource="{Binding mySource}">
<ListView.View>
<GridView>
<GridViewColumn Header="Filename" local:MainWindow.SortName="Model.filename" width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="0" Text="{Binding DisplayFilename}" VerticalAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Filesize" local:MainWindow.SortName="Model.FileSize" width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="0" Text="{Binding DisplayFileSize}" VerticalAlignment="Center" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
GridViewColumn 现在具有 Auto 宽度,并且 Textblock 中的宽度已被删除。现在文本不会立即消失。它只有在空间没有空间时才开始消失。