0

我使用Listpicker来允许用户选择颜色。 所以我使用Toolkit Listpicker和包含文本框的DataTemplate来显示它的列表项。我想要的是当页面加载时,先前选择的颜色(项目)被自动选中。但它给了我一个明显的'System.InvalidOperationException'异常,因为这些项目不是简单地添加,而是通过datatemplate文本框添加的。请大家帮帮我:带有DataTemplate的ListPicker的SelectedIndex或SelectedItem


<toolkit:ListPicker x:Name="BackgroundColor" FullModeHeader="Select Background Color:" Header="Background Color:" BorderThickness="0" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" ItemTemplate="{StaticResource PickerItemTemplate}" Background="#FF09043C" SelectionChanged="BackgroundColor_SelectionChanged" > 

      </toolkit:ListPicker> 

      <phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Name="PickerItemTemplate"> 
     <TextBlock Text="{Binding BackGroundColorString}" /> 
    </DataTemplate> 

    <DataTemplate x:Name="PickerFullModeItemTemplate" > 
     <Grid x:Name="rootGrid" Margin="0"> 

      <StackPanel Orientation="Horizontal"> 
       <TextBlock Name="BackgroundColor" 
        Text="{Binding BackGroundColorString}" 
          /> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 
        </phone:PhoneApplicationPage.Resources> 

if (SunderGutkaSettings.Contains(SG_KEY_BACKGROUNDCOLOR)) //check if key is not present, read value 
     { 
      if (BackGroundColorList.Count != 0)//test if list if not empty 
      { 

       var ListPickerBackGroundColorRead = BackGroundColorList[singletonInstance.SelectedFontColorIndex] as BackGroundlistPickerClass; //pull whole class 

       string ListPickerBackGroundColorReadString = ListPickerBackGroundColorRead.BackGroundColorString; 

       int ListPickerBackGroundColorReadStringToIndex = BackGroundColorList.FindIndex(x => x.BackGroundColorString.StartsWith(ListPickerBackGroundColorReadString)); 

       BackgroundColor.SelectedIndex = ListPickerBackGroundColorReadStringToIndex; //DISABLE FOR testing      


      } 

事实上,在simplyfing我的代码: BackgroundColor.SelectedIndex = 0;不工作 也不 BackgroundColor.SelectedItem =“红色”;

帮助我

+0

你的问题不清楚!你想做什么? – Sajeetharan 2014-09-06 06:58:21

+0

如何设置具有DataTemplate的ListPicker的SelectedItem? – 2014-09-06 06:59:28

+0

而你的ItemsSource是什么? – 2014-09-06 08:09:11

回答

0

设置的项目,

This.BackgroundColor.SelectedItem = YourObject; 

这是你如何让ListPicker的selectedItem属性,也许你需要投给您绑定到列表中选择器

private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var item = (sender as ListPicker).SelectedItem; 
    } 
对象
相关问题