【问题标题】:ListPicker Items.ClearListPicker Items.Clear
【发布时间】:2014-01-27 16:48:21
【问题描述】:

我在 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.

知道我做错了什么吗?

【问题讨论】:

    标签: c# xaml data-binding windows-phone-8 windows-phone


    【解决方案1】:

    当listpicker的ItemsSource与一个dataSource绑定时,Items已经是一个只读集合。你可以这样做: Paises.ItemsSource = null;但不是:Items.Clear()。希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2015-06-08
      • 2014-12-31
      相关资源
      最近更新 更多