• wpf中listview内容居中显示


     在WPF中使用ListView经常会用到GridView作为视图,但是却碰到GridViewColumn不能居中对齐的问题,

    实现方法

    给ListViewItem设置Style,让ListViewItem在水平方向拉伸填充:

     
           


     
    然后再给GridViewColumn的Datatemplate设置居中对齐就可以了。 

    实例代码

    1. <ListView x:Name="_detectionListView"
    2. ItemsSource="{Binding DetectionCheckList}"
    3. Background="White"
    4. BorderThickness="0"
    5. ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    6. <ListView.ItemContainerStyle>
    7. <Style TargetType="ListViewItem">
    8. <Setter Property="Background" Value="White" />
    9. <Setter Property="Foreground" Value="#464457" />
    10. <Setter Property="FontSize" Value="{Binding dataGridTextSize, Source={StaticResource ThemeCore}}" />
    11. <Setter Property="FontWeight" Value="Regular" />
    12. <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    13. <Style.Triggers>
    14. <Trigger Property="IsMouseOver" Value="True">
    15. <Setter Property="Background" Value="White" />
    16. <Setter Property="BorderBrush" Value="White" />
    17. </Trigger>
    18. <Trigger Property="IsSelected" Value="True">
    19. <Setter Property="Background" Value="White" />
    20. <Setter Property="BorderBrush" Value="White" />
    21. </Trigger>
    22. </Style.Triggers>
    23. </Style>
    24. </ListView.ItemContainerStyle>
    25. <ListView.View>
    26. <GridView AllowsColumnReorder="False">
    27. <GridView.ColumnHeaderContainerStyle>
    28. <Style TargetType="GridViewColumnHeader">
    29. <Setter Property="HorizontalContentAlignment" Value="Center"/>
    30. <Setter Property="VerticalAlignment" Value="Center"/>
    31. <Setter Property="SnapsToDevicePixels" Value="True" />
    32. <Setter Property="Height" Value="48" />
    33. <Setter Property="Background" Value="#F8F8F8"/>
    34. <Setter Property="Opacity" Value="0.82"/>
    35. <Setter Property="FontWeight" Value="Semibold"/>
    36. <Setter Property="Foreground" Value="#464457"/>
    37. <Setter Property="FontSize" Value="{Binding dataGridTextSize, Source={StaticResource ThemeCore}}"/>
    38. </Style>
    39. </GridView.ColumnHeaderContainerStyle>
    40. <GridViewColumn Header="设备名称" Width="{Binding ElementName=col1,Path=ActualWidth}" >
    41. <GridViewColumn.CellTemplate>
    42. <DataTemplate>
    43. <Grid>
    44. <TextBlock Text="{Binding deviceName}" HorizontalAlignment="Center" TextAlignment="Center"/>
    45. </Grid>
    46. </DataTemplate>
    47. </GridViewColumn.CellTemplate>
    48. </GridViewColumn>
    49. <GridViewColumn Header="序列号" Width="{Binding ElementName=col2,Path=ActualWidth}">
    50. <GridViewColumn.CellTemplate>
    51. <DataTemplate>
    52. <TextBlock Text="{Binding serialNumber}" HorizontalAlignment="Center"/>
    53. </DataTemplate>
    54. </GridViewColumn.CellTemplate>
    55. </GridViewColumn>
    56. <GridViewColumn Header="IMEI" Width="{Binding ElementName=col3,Path=ActualWidth}">
    57. <GridViewColumn.CellTemplate>
    58. <DataTemplate>
    59. <TextBlock Text="{Binding deviceIMEI}" HorizontalAlignment="Center"/>
    60. </DataTemplate>
    61. </GridViewColumn.CellTemplate>
    62. </GridViewColumn>
    63. <GridViewColumn Header="设备型号" Width="{Binding ElementName=col4,Path=ActualWidth}">
    64. <GridViewColumn.CellTemplate>
    65. <DataTemplate>
    66. <TextBlock Text="{Binding deviceModel}" HorizontalAlignment="Center"/>
    67. </DataTemplate>
    68. </GridViewColumn.CellTemplate>
    69. </GridViewColumn>
    70. <GridViewColumn Header="检测时间" Width="{Binding ElementName=col5,Path=ActualWidth}">
    71. <GridViewColumn.CellTemplate>
    72. <DataTemplate>
    73. <TextBlock Text="{Binding operateTime}" HorizontalAlignment="Center"/>
    74. </DataTemplate>
    75. </GridViewColumn.CellTemplate>
    76. </GridViewColumn>
    77. <GridViewColumn Header="耗时" Width="{Binding ElementName=col6,Path=ActualWidth}">
    78. <GridViewColumn.CellTemplate>
    79. <DataTemplate>
    80. <TextBlock Text="{Binding checkTimeCost}" HorizontalAlignment="Center"/>
    81. </DataTemplate>
    82. </GridViewColumn.CellTemplate>
    83. </GridViewColumn>
    84. <GridViewColumn Header="次数" Width="{Binding ElementName=col7,Path=ActualWidth}">
    85. <GridViewColumn.CellTemplate>
    86. <DataTemplate>
    87. <TextBlock Text="{Binding Violation_Count}" HorizontalAlignment="Center"/>
    88. </DataTemplate>
    89. </GridViewColumn.CellTemplate>
    90. </GridViewColumn>
    91. <GridViewColumn Header="检测状态" Width="{Binding ElementName=col8,Path=ActualWidth}">
    92. <GridViewColumn.CellTemplate>
    93. <DataTemplate>
    94. <TextBlock Text="{Binding DetectionStatus}" HorizontalAlignment="Center"/>
    95. </DataTemplate>
    96. </GridViewColumn.CellTemplate>
    97. </GridViewColumn>
    98. <GridViewColumn Header="USB位置" Width="{Binding ElementName=col9,Path=ActualWidth}">
    99. <GridViewColumn.CellTemplate>
    100. <DataTemplate>
    101. <TextBlock Text="{Binding UsbPostionInfo}" HorizontalAlignment="Center"/>
    102. </DataTemplate>
    103. </GridViewColumn.CellTemplate>
    104. </GridViewColumn>
    105. <GridViewColumn Header="进度" Width="{Binding ElementName=col10,Path=ActualWidth}">
    106. <GridViewColumn.CellTemplate>
    107. <DataTemplate>
    108. <TextBlock Text="{Binding DetectStep}" HorizontalAlignment="Center"/>
    109. </DataTemplate>
    110. </GridViewColumn.CellTemplate>
    111. </GridViewColumn>
    112. </GridView>
    113. </ListView.View>
    114. </ListView>

  • 相关阅读:
    负担过重何以轻装前行
    【PHPWord】基于ThinkPHP6的Word模板处理和导出PDF文件的带源码 | 答读者问
    Day6-创造[哈希表]:array,set,map
    django安装到运行,简单的注册登录
    C++位图
    前端面试宝典React篇13 如何分析和调优性能瓶颈?
    设计模式(二)-创建者模式(1)-单例模式
    vue实现调用手机拍照、录像功能
    HTTPS && Tomcat && Servlet && 博客系统 && 软件测试的概念 && Linux
    MongoDB基础
  • 原文地址:https://blog.csdn.net/babytiger/article/details/133763454