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; 
    } 
對象
相關問題