0

I created the following ListView to display some data (removed extraneous markup):

<ListView ItemsSource="{Binding NewYorkResidents}">
    <ListView.Header>
        <Style>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
        </Style>
    </ListView.Header>
    <ListView.HeaderTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="Name" Grid.Column="0" />
                <TextBlock Text="Address" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ListView.HeaderTemplate>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" Grid.Column="0" />
                <TextBlock Text="{Binding Address}" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Now I want to reuse this exact same ListView + markup in a different view, just with a different ItemsSource (though it will bind to the same data type).

What is the best way to reuse the ListView and just specifiy the ItemsSource? I'm hoping to be able to do something like this:

<ListView DataTemplate="MyTemplate" ItemsSource=<some new binding> />

and still have it show the ListView headers and Name and Address TextBlocks using data from the ItemsSource.

Making a ControlTemplate doesn't seem like the right thing because I am specifiying actual data in the list view also (such as binding to name and address).

Is there a better way to create some type of resource so I can reuse this?

JT Smith
  • 361
  • 1
  • 2
  • 15
  • you can use controltemplate for this scenario, for binding you can use TemplateBinding or RelativeSource. https://stackoverflow.com/q/1131222/2745294 – Sushil Mate Jul 04 '17 at 04:14

1 Answers1

0

Define the header template and item template in resource dictionary and add reference of them in your code. you can reuse this templates.

   <DataTemplate x:Key="HeaderTemplate1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="Name" Grid.Column="0" />
            <TextBlock Text="Address" Grid.Column="1" />
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="ListViewTemplate1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Name}" Grid.Column="0" />
            <TextBlock Text="{Binding clothing1}" Grid.Column="1" />
        </Grid>
    </DataTemplate>

  <ListView HeaderTemplate="{StaticResource HeaderTemplate1}" ItemTemplate="{StaticResource ListViewTemplate1}"/>

for more information on Item template: https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/listview-item-templates

Mohanvel V
  • 768
  • 4
  • 17