c# - 如何在不创建 List<string> 的情况下将图像源绑定(bind)到路径列表?

标签 c# wpf xaml windows-phone-8 windows-phone-8.1

我想创建一个包含描述和几张图片(作为标签)的 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>

以及用于绑定(bind)的类的 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”!

我认为将所有路径存储在每个国家/地区列表中会浪费大量空间,因为所有图像路径都是重复的。图像源是否需要绑定(bind)列表?或者有没有其他方法可以实现图片源绑定(bind),源数据量小,重复频率高?

最佳答案

当您在 GridView 中使用 DataTemplate 时,您可以使用 TemplateSelector 作为替代方法。根据需要在 Xaml 中使用源代码编写不同的 DataTemplates,然后在运行时使用 contentPresenter 将该模板添加到您的 ListView DataTemplate。

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

</DataTemplate>

关于c# - 如何在不创建 List<string> 的情况下将图像源绑定(bind)到路径列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31933192/

相关文章:

C#成员变量初始化;最佳实践?

c# - 如何使用 DataTemplates 通过 ViewModel-First 方法缓存动态切换的 View

wpf - 使用子元素创建 WPF 用户控件

c# - 未能成功地将我的 ViewModel 数据公开给我的 XAML 绑定(bind) View

wpf - 自动调整大小按钮样式

wpf - 为什么特殊字符是无效的按钮内容?

c# - 如何阅读文本框中的最后一行?

c# - IDictionary 与哈希表 -

c# - 为什么列表中有一个数组? C#

c# - WPF:通过自定义依赖属性绑定(bind)