2014-04-14 84 views
0

在以下http://www.geekchamp.com/articles/listpicker-for-wp7-in-depth我想設置我的ListPicker使用自定義類類型的列表填充,但出於某種原因,我沒有看到我的項目在視圖中綁定。我有什麼是如何創建ListPicker的ItemTemplate

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; 
    } 
+0

的ItemTemplate =「{StaticRessource yourDataTemplate} – MatDev8

回答

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"/> 
+0

我無法分辨原始版本和更新版本之間的區別離子? – Matthew

+0

{StaticResource代替{分配模板時綁定 – csharpwinphonexaml