如何在不创建 List<string> 的情况下将图像源绑定到路径列表?
How to bind image source to a list of path without creating a List<string>?
我想创建一个包含描述和几张图片(作为标签)的 ListView。
例如:
item1 USA image1 image2 image3
item2 Canada image2 image3 image4
item3 France image1 image3 image4
由于这些图像只是标签,因此可能会重复。
这个ListView的xaml是
<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>
和 class 的 C# 代码用于绑定:
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;
}
}
主程序C#代码:
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;
}
在这个例子中,image3出现在三个国家,所以它的路径应该存储在三个不同的列表中。但是,所有这些路径都是相同的,"Assets/Image3" !
我认为将所有路径存储在各个国家/地区的每个列表中会浪费很多 space,因为所有图像路径都是重复的。图像源是否需要绑定列表?或者有没有其他方法可以让图片源绑定源数据量小但重复频率高?
当您在 GridView 中使用 DataTemplate 时,您可以使用 TemplateSelector 作为替代方法。根据需要在 Xaml 中使用源编写不同的 DataTemplates,然后在运行时使用 contentPresenter 将该模板添加到您的 ListView DataTemplate。
<DataTemplate>
<ContentPresenter
ContentTemplateSelector="{StaticResource ResourceKey=myImageTemplateSelector}">
</ContentPresenter>
</DataTemplate>
我想创建一个包含描述和几张图片(作为标签)的 ListView。
例如:
item1 USA image1 image2 image3
item2 Canada image2 image3 image4
item3 France image1 image3 image4
由于这些图像只是标签,因此可能会重复。
这个ListView的xaml是
<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>
和 class 的 C# 代码用于绑定:
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;
}
}
主程序C#代码:
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;
}
在这个例子中,image3出现在三个国家,所以它的路径应该存储在三个不同的列表中。但是,所有这些路径都是相同的,"Assets/Image3" !
我认为将所有路径存储在各个国家/地区的每个列表中会浪费很多 space,因为所有图像路径都是重复的。图像源是否需要绑定列表?或者有没有其他方法可以让图片源绑定源数据量小但重复频率高?
当您在 GridView 中使用 DataTemplate 时,您可以使用 TemplateSelector 作为替代方法。根据需要在 Xaml 中使用源编写不同的 DataTemplates,然后在运行时使用 contentPresenter 将该模板添加到您的 ListView DataTemplate。
<DataTemplate>
<ContentPresenter
ContentTemplateSelector="{StaticResource ResourceKey=myImageTemplateSelector}">
</ContentPresenter>
</DataTemplate>