加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

WPFGridViewColumn对齐不起作用

(2023-08-20 22:57:03)
分类: c#c c
【解决方案1】:

如果您只希望一个特定的列向右对齐,请尝试以下示例

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的HorizontalContentAlignment属性为Stretch,这样每个ListViewItem中的元素可以跨越或沿着每列的整个宽度定位。因为 ListView 绑定到数据源,所以需要创建一个样式来设置 HorizontalContentAlignment。接下来,您需要使用 DataTemplate 来显示内容,而不是使用 DisplayMemberBinding 属性。要显示每个模板的值,DataTemplate 可以只包含一个 TextBlock,其 HorizontalAlignment 属性设置为 Right。 以下示例定义了使列右对齐所需的样式和 DataTemplate,并更改 GridViewColumn 以引用 DataTemplate。

【讨论】:

【解决方案2】:

这里的问题是ListViewItem(即每个GridViewColumn 单元格的容器)。 您必须将其属性HorizontalContentAlignment 设置为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。不过我很欣赏你的尝试

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有