2011-11-17 67 views
2

我在尝试将创建的对象绑定到listpicker时遇到问题。我已经成功地使用了带有字符串和整数的listpicker,但是在尝试使用我自己的类/对象时遇到了问题。Listpicker绑定问题WP7

这里是XML(我有两个listpickers,一个作品,一个没有)

      <toolkit:ListPicker 
          x:Name="CountryListPicker" 
          Margin="0,2,0,10" Width="458" 
          BorderThickness="3" FullModeHeader="Country" 
          CacheMode="BitmapCache"> 
          <toolkit:ListPicker.ItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <Border Background="LightBlue" Width="34" Height="34"> 
              <TextBlock Text="{Binding _code}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
             </Border> 
             <TextBlock Text="{Binding _name}" Margin="12 0 0 0"/> 
            </StackPanel> 
           </DataTemplate> 
          </toolkit:ListPicker.ItemTemplate> 
          <toolkit:ListPicker.FullModeItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal" Margin="0 21 0 20"> 
             <TextBlock Text="{Binding _name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/> 
            </StackPanel> 
           </DataTemplate> 
          </toolkit:ListPicker.FullModeItemTemplate> 
         </toolkit:ListPicker> 

         <toolkit:ListPicker 
          x:Name="testPicker" 
          Header="Accent color" 
          FullModeHeader="ACCENTS" 
          CacheMode="BitmapCache"> 
          <toolkit:ListPicker.ItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="{Binding}" Margin="12 0 0 0"/> 
            </StackPanel> 
           </DataTemplate> 
          </toolkit:ListPicker.ItemTemplate> 
          <toolkit:ListPicker.FullModeItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal" Margin="0 21 0 20"> 
             <TextBlock Text="{Binding}" 
            Margin="16 0 0 0" 
            FontSize="43" 
            FontFamily="{StaticResource PhoneFontFamilyLight}"/> 
            </StackPanel> 
           </DataTemplate> 
          </toolkit:ListPicker.FullModeItemTemplate> 
         </toolkit:ListPicker>` 

第一个列表选择器是我的新对象绑定一个,第二个是绑定的字符串。

这里是后面的代码:

 ObservableCollection<Country> countries = new ObservableCollection<Country>(); 
     countries.Add(new Country { _code = "US", _name = "United States1"}); 
     countries.Add(new Country { _code = "US", _name = "United States2" }); 
     countries.Add(new Country { _code = "US", _name = "United States3" }); 
     countries.Add(new Country { _code = "US", _name = "United States4" }); 
     countries.Add(new Country { _code = "US", _name = "United States5" }); 
     countries.Add(new Country { _code = "US", _name = "United States6" }); 
     this.CountryListPicker.ItemsSource = new ReadOnlyCollection<Country>(countries); 

     ObservableCollection<string> _accentColors = new ObservableCollection<string>(); 
     _accentColors.Add("Blue"); 
     _accentColors.Add("Blue2"); 
     _accentColors.Add("Blue3"); 
     _accentColors.Add("Blue4"); 
     _accentColors.Add("Blue5"); 
     _accentColors.Add("Blue6"); 
     _accentColors.Add("Blue7"); 
     this.testPicker.ItemsSource = new ReadOnlyCollection<string>(_accentColors); 

第二届listpicker是很好,我想这是因为它含有的字符串。

我破碎的listpicker显示所有正确的数据,但是当我选择一个listpickeritem时,它不会选择它。该应用程序加载完整的listpicker,然后单击其中一个项目,当我返回主视图时,所选项目不会反映在listpicker中。

任何人有任何想法?需要我解释了吗?

+0

嗨,这里有一个wp7工具包的新版本,它修复了很多'ListPicker'中的错误,你可能想去看看。 :) http://silverlight.codeplex.com/releases/view/75888 –

+0

谢谢,但我使用最新的。我认为更新会解决它,但它只能解决滚动问题。我的对象是否必须实现某种接口来通知UI发生了选择? – garry

+0

我还在CodePlex上发布了这个问题,供那些想要关注此问题的人使用。如果他们为我工作,我会在这里发布任何答案。 http://silverlight.codeplex.com/workitem/9839 – garry

回答

2

我自己想出了这一个。我有一个加载函数,其中listpicker正在初始化。看起来像Loaded在列表选择器从全屏视图返回并重新设置selectedIndex时得到“加载”。修复很简单,只需移动列表的创建以及将Loaded函数中的itemSource设置为构造函数即可。