Live Chat Icon For mobile
Live Chat Icon

How do I create a single template and resue it for multiple columns in a ListView?

Platform: WPF| Category: ListView

Here is a solution:

[XAML]
<Page

    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'

    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'

    xmlns:sys='clr-namespace:System;assembly=mscorlib'>

  <Page.Resources>

    <XmlDataProvider x:Key='CharacterData'>

      <x:XData>

        <Data xmlns=''>

          <Character First='Bart' Last='Simpson' Gender='M'/>

          <Character First='Homer' Last='Simpson' Gender='M'/>

          <Character First='Lisa' Last='Simpson' Gender='F'/>

          <Character First='Maggie' Last='Simpson' Gender='F'/>

          <Character First='Marge' Last='Simpson' Gender='F'/>

        </Data>

      </x:XData>

    </XmlDataProvider>

    <DataTemplate x:Key='LVTSimpleText'>

      <Border Width='Auto' Height='Auto' BorderBrush='#FF000000' 

          BorderThickness='0,0,2,2'>

        <TextBox Width='Auto' Height='Auto' Text='{Binding Value}' 

            TextWrapping='Wrap'/>

      </Border>

    </DataTemplate>

  </Page.Resources>

  <ListView ItemsSource='{Binding Source={StaticResource CharacterData},

      XPath=Data/Character}'>

    <ListView.View>

      <GridView>

        <GridViewColumn Header='First Name'>

          <GridViewColumn.CellTemplate>

            <DataTemplate>

              <ContentControl Content='{Binding XPath=@First}' 

                  ContentTemplate='{StaticResource LVTSimpleText}' />

            </DataTemplate>

          </GridViewColumn.CellTemplate>

        </GridViewColumn>

        <GridViewColumn Header='Last Name'>

          <GridViewColumn.CellTemplate>

            <DataTemplate>

              <ContentControl Content='{Binding XPath=@Last}' 

                  ContentTemplate='{StaticResource LVTSimpleText}' />

            </DataTemplate>

          </GridViewColumn.CellTemplate>

        </GridViewColumn>

        <GridViewColumn Header='Gender'>

          <GridViewColumn.CellTemplate>

            <DataTemplate>

              <ContentControl Content='{Binding XPath=@Gender}' 

                  ContentTemplate='{StaticResource LVTSimpleText}' />

            </DataTemplate>

          </GridViewColumn.CellTemplate>

        </GridViewColumn>

      </GridView>

    </ListView.View>

  </ListView>

</Page>


Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.