2014-01-27 26 views
0

我有一個Windows Phone 8的項目如下ListPickers:ListPicker Items.Clear

<toolkit:ListPicker Name="Continentes" ExpansionMode="ExpansionAllowed" Grid.Row="1" Width="220" Margin="20,20,261,14" Background="White" Foreground="Black" SelectionChanged="Continentes_SelectionChanged" Canvas.ZIndex="10"> 
</toolkit:ListPicker> 

<Image Source="/Assets/Images/[email protected]" Width="16.5" Height="10.5" Margin="187,49,276,567" Grid.Row="1" Canvas.ZIndex="10" /> 

<toolkit:ListPicker Name="Paises" Grid.Row="1" Width="220" Margin="249,20,20,20" Background="White" Foreground="Black" Canvas.ZIndex="10"> 
</toolkit:ListPicker> 

第一ListPicker是過得去的字符串列表填充,那麼就說明大陸的列表:

Europe, 
Asia, 
India... 

當我選擇其中一個大洲時,第二個選取器將使用以下函數填充包含國家/地區的字符串列表,該函數在第一個選取器上使用SelectionChanged事件處理函數激活:

private void Continentes_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var selectedItem = (sender as ListPicker).SelectedItem; 
    int selindex = Continentes.SelectedIndex; 
    List<string> listaDePaises = new List<string>(); 

    if (selindex != -1) 
    { 
     if ((string)selectedItem == "Europa") 
     { 
      Paises.Items.Clear(); 
      listaDePaises = countrys.getPaisesByName("Europa"); 
      Paises.ItemsSource = listaDePaises; 
     } 

     if ((string)selectedItem == "Asia") 
     { 
      Paises.Items.Clear(); 
      listaDePaises = countrys.getPaisesByName("Asia"); 
      Paises.ItemsSource = listaDePaises; 
     } 
    } 
} 

我第一次做continente selection,第二選擇器被填充,但我想它的第二次,我有以下異常,在Items.Clear()方法。

An exception of type 'System.InvalidOperationException' occurred in System.Windows.ni.dll but was not handled in user code 

Additional information: Operation not supported on read-only collection. 

什麼我做錯了任何想法?

回答

2

當listSicker的ItemsSource與dataSource綁定在一起時,Items已經是一個只讀集合。你可以這樣做:Paises.ItemsSource = null;但不是:Items.Clear()。我希望它能幫助你。

+0

真棒回答老兄。有效。謝謝。 +1 –