Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView GridView DisplayMemberBinding Center alignin content

I'm trying to center align data in a ListView/Gridview where I use DisplayMemberBinding.

This is (partially) how my gridview looks:

<ListView>
    <ListView.View>
        <GridView>
            <GridView.Columns>
               <GridViewColumn DisplayMemberBinding="{Binding Path=Timi}" Width="Auto">
                   <GridViewColumnHeader Content="Tími" Click="GridViewColumnHeader_Click" ></GridViewColumnHeader>
                </GridViewColumn>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

I tried to use CellTemplate like explained on this page http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumn.celltemplate.aspx but it didn't work and then later I read somewhere that one should never use cellTemplate and DisplayMemberBinding together.

So the question is : How do I center the gridview data when I use DisplayMemberBinding to bind the data to the gridview?

Thanks in advance.

like image 666
Bigginn Avatar asked Dec 17 '22 00:12

Bigginn


2 Answers

I figured out how to do this, but I had to alter a lot.
I followed the info on this link:
How to autosize and right-align GridViewColumn data in WPF?

first of all I had to add this into my Resource file :

<Style TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>

and then change my listview so it looked like this:

<ListView >
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn>
                     <GridViewColumnHeader Content="ColName" Click="GridViewColumnHeader_Click" ></GridViewColumnHeader>
                     <GridViewColumn.CellTemplate>
                         <DataTemplate>
                             <TextBlock Text="{Binding Path=col1, StringFormat='0.00'}" TextAlignment="Center" />
                         </DataTemplate>
                         </GridViewColumn.CellTemplate>
                     </GridViewColumn>
                 <GridView.Columns>
            <GridView>
    <ListView.View>
<ListView >

Thanks for the help
Bigginn

like image 53
Bigginn Avatar answered Jun 02 '23 18:06

Bigginn


This is much simpler and you can still use DisplayMemberBinding tag.

<ListView ItemsSource="{Binding Song}" TextBlock.FontSize="32">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}"/>
            <GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}"/>
        </GridView>
    </ListView.View>
</ListView>
like image 45
Erwin Zhang Avatar answered Jun 02 '23 18:06

Erwin Zhang