Dailycode.info

Short solution for short problems

Combining 2 or more columns in a listview using CellTemplate in stead of DisplayMemberBinding

Combining 2 or more columns in a listview is really simple actually. Using the DisplayMemberBinding

Property won’t get you there. This only allows 1 column to be bound. But using the CellTemplate property, you have unlimited possibilities. Just define the data template in a resource for example and bind it to the CellTemplate property. Here’s a simple example:

 

<GridViewColumn

Header="Country"

DisplayMemberBinding="{Binding Path=Country.CountryName}"

/> 

<GridViewColumn

Header="Farmer"

CellTemplate="{StaticResource FarmerTemplate}"

/>

 

 

The data template is defined in a resource file and looks like this:

 

<DataTemplate x:Key="FarmerTemplate">

    <DockPanel>

        <TextBlock HorizontalAlignment="Left">

            <TextBlock.Text>

            <Binding Path="Farmer.LastName"/>

            </TextBlock.Text>

        </TextBlock>

        <TextBlock Text=" "></TextBlock>

        <TextBlock HorizontalAlignment="Left">

            <TextBlock.Text>

            <Binding Path="Farmer.FirstName"/>

            </TextBlock.Text>

        </TextBlock>

    </DockPanel>

</DataTemplate>

 

The result: