我在Windows Phone 8项目上有以下ListPickers:
<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.
对我做错了什么的想法?
答案 0 :(得分:3)
当listpicker的ItemsSource与dataSource绑定时,Items已经是只读集合。你可以这样做:Paises.ItemsSource = null;但不是:Items.Clear()。我希望它可以帮到你。