如何创建ListPicker的ItemTemplate

时间:2014-04-14 08:43:20

标签: c# xaml windows-phone-8 listpicker

在下面http://www.geekchamp.com/articles/listpicker-for-wp7-in-depth我试图设置我的ListPicker使用自定义类类型的List进行填充,但由于某种原因,我没有看到我的项目在视图中绑定。我拥有的是

MainPage.xaml中

<Grid.Resources>
        <DataTemplate x:Name="SearchProviderItemTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Favicon}" />
                <TextBlock Text="{Binding Name}" Margin="12,0,0,0"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Name="SearchProviderFullModeItemTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Favicon}" />
                <TextBlock Text="{Binding Name}" Margin="12,0,0,0"/>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>

...

<toolkit:ListPicker x:Name="SearchProviderListPicker" 
                            Header="{Binding Path=LocalizedResources.SettingsPage_SearchProvider, Source={StaticResource LocalizedStrings}}" ItemTemplate="{Binding SearchProviderItemTemplate}"
                            FullModeHeader="{Binding Path=LocalizedResources.SettingsPage_SearchProvider, Source={StaticResource LocalizedStrings}}" FullModeItemTemplate="{Binding SearchProviderFullModeItemTemplate}"
                            SelectedIndex="{Binding}"
                            SelectionChanged="SearchProviderListPicker_SelectionChanged" 
                            CacheMode="BitmapCache"/>

MainPage.xaml.cs中

List<ListItem> searchProviderList;

public MainPage()
    {
        InitializeComponent();

        PopulateSearchProviderList();
    }

    private void PopulateSearchProviderList()
    {
        searchProviderList = new List<ListItem>();

        searchProviderList.Add(new ListItem { Favicon = "", Name = "Bing", Address = "http://www.bing.com" });
        searchProviderList.Add(new ListItem { Favicon = "", Name = "Google", Address = "http://www.google.com" });

        SearchProviderListPicker.ItemsSource = searchProviderList;
    }

出于某些原因,我在ListPicker中看到的只是TestApp.Classes.ListItem。我创建的类ListItem只是以下

ListItem.cs

public string Favicon
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public string Address
    {
        get;
        set;
    }

1 个答案:

答案 0 :(得分:0)

改变这个:

<toolkit:ListPicker x:Name="SearchProviderListPicker" 
                        Header="{Binding Path=LocalizedResources.SettingsPage_SearchProvider, Source={StaticResource LocalizedStrings}}" ItemTemplate="{Binding SearchProviderItemTemplate}"
                        FullModeHeader="{Binding Path=LocalizedResources.SettingsPage_SearchProvider, Source={StaticResource LocalizedStrings}}" FullModeItemTemplate="{Binding SearchProviderFullModeItemTemplate}"
                        SelectedIndex="{Binding}"
                        SelectionChanged="SearchProviderListPicker_SelectionChanged" 
                        CacheMode="BitmapCache"/>

到此:

<toolkit:ListPicker x:Name="SearchProviderListPicker" 
                        Header="{Binding Path=LocalizedResources.SettingsPage_SearchProvider, Source={StaticResource LocalizedStrings}}" 
                        ItemTemplate="{StaticResource SearchProviderItemTemplate}"
                        FullModeHeader="{Binding Path=LocalizedResources.SettingsPage_SearchProvider, Source={StaticResource LocalizedStrings}}" 
                        FullModeItemTemplate="{StaticResource SearchProviderFullModeItemTemplate}"
                        SelectedIndex="{Binding}"
                        SelectionChanged="SearchProviderListPicker_SelectionChanged" 
                        CacheMode="BitmapCache"/>