ListPicker Items.Clear



我在Windows Phone 8项目上有以下列表:

<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/arrow@2x.png" 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.

对我做错了什么的想法?

当listPicker的项目源用数据源镶嵌时,这些项目已经是只读的集合。您可以这样做:paises.itemssource = null;但不是:items.crear()。我希望它可以帮助您。

最新更新