0
votes

I try to clear (and add) items to ListPicker, but when the app have to clear all of the items is an error - "SelectedItem must always be set to a valid value". My code:

<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>

and action for a button

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]);
    }
}

I try another options, but it doesn't work too.

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
Are you trying to clear it before you've added any items? Or just after you've added items to it? - philorube
After added. This is "changing" items... - SlaviS
Instead of clearing and adding one by one can you simply do this? select.Items = arrays - philorube
Didn't working. When I did it are errors - "Cannot implicitly convert type 'string[]' to 'System.Windows.Controls.ItemCollection'" and "Property or indexer 'System.Windows.Controls.ItemsControl.Items' cannot be assigned to -- it is read only" Any ideas? - SlaviS

1 Answers

0
votes

To clear the selection you should use:

select.SelectedItems.Clear();