WPFGridViewColumn对齐不起作用
(2023-08-20 22:57:03)分类: c#c c |
如果您只希望一个特定的列向右对齐,请尝试以下示例
https://msdn.microsoft.com/en-us/library/bb961985.aspx
这是我的实现
<<span style="color:rgb(255 211 0)">Window
.Resources> <<span style="color:rgb(255 95 0)">Style TargetType="ListViewItem"> <<span style="color:rgb(255 211 0)">Setter Property="HorizontalContentAlignment " Value="Stretch" /> </<span style="color:rgb(255 95 0)">Style> </<span style="color:rgb(255 211 0)">Window.Resources> <<span style="color:rgb(73 238 255)">Grid> <<span style="color:rgb(98 189 255)">ListView ItemsSource="{Binding VolumeNumber}"> <<span style="color:rgb(98 189 255)">ListView.View> <<span style="color:rgb(73 238 255)">GridView> <<span style="color:rgb(98 189 255)">GridViewColumn Header="SomeStatus" DisplayMemberBinding="{Binding Name}" Width="170"/> <<span style="color:rgb(98 189 255)">GridViewColumn Header="from" Width="170"> <<span style="color:rgb(98 189 255)">GridViewColumn.CellTemplate> <<span style="color:rgb(98 189 255)">DataTemplate> <<span style="color:rgb(98 189 255)">TextBlock Text="{Binding Value}" TextAlignment="Right" /> </<span style="color:rgb(98 189 255)">DataTemplate> </<span style="color:rgb(98 189 255)">GridViewColumn.CellTemplate> </<span style="color:rgb(98 189 255)">GridViewColumn> </<span style="color:rgb(73 238 255)">GridView> </<span style="color:rgb(98 189 255)">ListView.View> </<span style="color:rgb(98 189 255)">ListView> </<span style="color:rgb(73 238 255)">Grid> </<span style="color:rgb(255 211 0)">Window>
现在不要在 GridViewColumn 声明中设置 DisplayMemberBinding="{Binding Value}",需要在文本块中设置 否则会忽略你的单元格模板
要改变列的对齐方式,需要指定每个ListViewItem的HorizontalContentAlignme
【讨论】:
这里的问题是ListViewItem
(即每个GridViewColumn
单元格的容器)。 您必须将其属性HorizontalContentAlignme
设置为Stretch
。
看看这里(如果你想使用 DataTemplate,你还需要删除
DisplayMemberBinding
):
<<span style="color:rgb(98 189 255)">ListView
ItemsSource="{Binding VolumeNumber}" HorizontalAlignment="Stretch" HorizontalContentAlignment ="Stretch" > <<span style="color:rgb(98 189 255)">ListView.ItemContainerStyle> <<span style="color:rgb(255 95 0)">Style TargetType="ListViewItem"> <<span style="color:rgb(255 211 0)">Setter Property="HorizontalContentAlignment " Value="Stretch" /> </<span style="color:rgb(255 95 0)">Style> </<span style="color:rgb(98 189 255)">ListView.ItemContainerStyle> <<span style="color:rgb(98 189 255)">ListView.View> <<span style="color:rgb(98 189 255)">GridView AllowsColumnReorder="False"> <<span style="color:rgb(98 189 255)">GridViewColumn Header="SomeStatus" DisplayMemberBinding="{Binding Name}" Width="170" /> <<span style="color:rgb(98 189 255)">GridViewColumn Header="from" Width="170"> <<span style="color:rgb(98 189 255)">GridViewColumn.CellTemplate> <<span style="color:rgb(98 189 255)">DataTemplate> <<span style="color:rgb(98 189 255)">TextBlock Text="{Binding Value, StringFormat=0.000000}" TextAlignment="Right" /> </<span style="color:rgb(98 189 255)">DataTemplate> </<span style="color:rgb(98 189 255)">GridViewColumn.CellTemplate> </<span style="color:rgb(98 189 255)">GridViewColumn> </<span style="color:rgb(98 189 255)">GridView> </<span style="color:rgb(98 189 255)">ListView.View> </<span style="color:rgb(98 189 255)">ListView>
【讨论】:
-
我也想让你的回答成为可能:) 给你加分
-
但是如果我觉得它对我有好处,我会浏览你的帖子并投票:)
-
不管@MikroDel。不过我很欣赏你的尝试