Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF GridViewColumn align doesnt work

Tags:

c#

wpf

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>
like image 731
MikroDel Avatar asked Aug 28 '26 00:08

MikroDel


2 Answers

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

like image 120
The One Avatar answered Aug 29 '26 13:08

The One


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>
like image 38
Il Vic Avatar answered Aug 29 '26 15:08

Il Vic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!