ListPicker“SelectedItem必须始终设置为有效值”

时间:2014-01-31 21:11:24

标签: c# windows-phone-8 listpicker

我尝试将项目清除(并添加)到ListPicker,但当应用程序必须清除所有项目时出错 - “SelectedItem必须始终设置为有效值”。我的代码:

<toolkit:ListPicker x:Name="select" HorizontalAlignment="Right" Margin="0,135,30,0" VerticalAlignment="Top" Width="195" Height="64" d:LayoutOverrides="HorizontalAlignment" BorderBrush="{x:Null}" FontFamily="Arial" FontWeight="Bold" FontSize="32" Style="{StaticResource ListPickerStyle1}" BorderThickness="0" DataContext="{Binding}">
    <toolkit:ListPicker.Background>
        <ImageBrush Stretch="Fill" ImageSource="list_picker.png"/>
    </toolkit:ListPicker.Background>

和按钮的操作

private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    select.Items.Clear(); //here is an error
    for (int i = 0; i < arrays.Length; i++)
    {
        select.Items.Add(arrays[i]);
    }
}

我尝试了其他选项,但它也不起作用。

private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
select.SelectedItem = null; // here is an error
    select.Items.Clear();
    for (int i = 0; i < arrays.Length; i++)
    {
        select.Items.Add(arrays[i]);
    }
}

1 个答案:

答案 0 :(得分:0)

要清除您应该使用的选择:

select.SelectedItems.Clear();