How to associate an image source with a path list without creating a <string> list?

I want to create a ListView that contains a description and several images (like tags).

For instance:

item1 USA image1 image2 image3 item2 Canada image2 image3 image4 item3 France image1 image3 image4 

Since these images are simply tags, they can be repeated.

xaml of this ListView

  <ListView x:Name="MyList"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock x:Name="Coutry" Text="{Binding CountryName}"/> <GridView> <GridView.ItemTemplate> <DataTemplate> <Image x:Name="CountryTags" Source="{Binding CountryProperty}"/> </DataTemplate> </GridView.ItemTemplate> </GridView> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> 

And C # code for binding:

 public class Country { public string CountryName { get; set; } // this list store image path for tags required for each country public List<string> CountryProperty { get; set; } public Country(string CountryName, List<string> CountryProperty) { this.CountryName = CountryName; this.CountryProperty = CountryProperty; } } 

Main program code:

  protected override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); List<Country> Countries = new List<Country>(); List<string> ImgPath = new List<string>(); // first country ImgPath.Add("Assets/Image1"); ImgPath.Add("Assets/Image2"); ImgPath.Add("Assets/Image3"); Countries.Add(new Country("USA", ImgPath)); // second country ImgPath.Add("Assets/Image2"); ImgPath.Add("Assets/Image3"); ImgPath.Add("Assets/Image4"); Countries.Add(new Country("Canada", ImgPath)); // third country ImgPath.Add("Assets/Image1"); ImgPath.Add("Assets/Image3"); ImgPath.Add("Assets/Image4"); Countries.Add(new Country("France", ImgPath)); // bind data MyList.ItemsSource = Countries; } 

In this example, image3 appears in three countries, so its path should be stored in three different lists. However, all of these paths are the same: "Assets / Image3"!

I think that there is a lot of space for storing all the paths in each list of individual countries, because the whole image path is repeated. Does the image source need to be associated with a list? Or are there any other ways for the image source to communicate with a small amount of source data, but a high repetition rate?

+4
source share
1 answer

Since you are using a DataTemplate in a GridView, you can use TemplateSelector as an alternative approach. Write various DataTemplates in Xaml with the source as needed, and then use contentPresenter to add this template to your ListView DataTemplate at run time.

 <DataTemplate> <ContentPresenter ContentTemplateSelector="{StaticResource ResourceKey=myImageTemplateSelector}"> </ContentPresenter> </DataTemplate> 
0
source

All Articles