6

I want the information in GridViewColumn "from" aligned to the right.

This is what I have done and its not working:

    <ListView ItemsSource="{Binding VolumeNumber}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" >
       <ListView.View>
          <GridView AllowsColumnReorder="False">
             <GridViewColumn Header="SomeStatus" DisplayMemberBinding="{Binding Name}" Width="170" />
             <GridViewColumn Header="from" DisplayMemberBinding="{Binding Value, StringFormat=0.000000}" Width="170 >
                <GridViewColumn.CellTemplate>
                   <DataTemplate>
                      <TextBlock TextAlignment="Right" Width="40"/>
                   </DataTemplate>
                </GridViewColumn.CellTemplate>
             </GridViewColumn>
          </GridView>
       </ListView.View>
    </ListView>
Peter Wishart
  • 11,600
  • 1
  • 26
  • 45
MikroDel
  • 6,705
  • 7
  • 39
  • 74

2 Answers2

12

If you want just one specific column aligned to the right try the example on

https://msdn.microsoft.com/en-us/library/bb961985.aspx

Here's my implementation

<Window.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding VolumeNumber}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="SomeStatus" DisplayMemberBinding="{Binding Name}" Width="170"/>
                <GridViewColumn Header="from" Width="170">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Value}" TextAlignment="Right" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
</Window>

Now DON'T SET DisplayMemberBinding="{Binding Value}" in the GridViewColumn declaration, you need to set it in the textblock Otherwise it will ignore your celltemplate

To change the alignment of the column, you need to specify that the HorizontalContentAlignment property of each ListViewItem is Stretch, so that the elements in each ListViewItem can span or be positioned along the entire width of each column. Because the ListView is bound to a data source, you need to create a style that sets the HorizontalContentAlignment. Next, you need to use a DataTemplate to display the content instead of using the DisplayMemberBinding property. To display the Value of each template, the DataTemplate can just contain a TextBlock that has its HorizontalAlignment property set to Right. The following example defines the style and DataTemplate necessary to make the column right-aligned, and changes the GridViewColumn to reference the DataTemplate.

enter image description here

The One
  • 4,560
  • 5
  • 36
  • 52
6

The problem here is the ListViewItem (i.e. the container of each GridViewColumn cell). You have to set its property HorizontalContentAlignment to Stretch.

Have a look here (you also need to remove the DisplayMemberBinding if you want to use a DataTemplate):

<ListView ItemsSource="{Binding VolumeNumber}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" >
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn Header="SomeStatus" DisplayMemberBinding="{Binding Name}" Width="170" />
            <GridViewColumn Header="from" Width="170">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Value, StringFormat=0.000000}" TextAlignment="Right" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
Il Vic
  • 5,576
  • 4
  • 26
  • 37