Search code examples
c#windows-phone-7windows-phone-8listpicker

ListPicker without selected item


I have a ListPicker and when this is opened, by default the first item is selected. In my app, user select an item on ListPicker, this item is sent to my database. The next time that user opens the picker, automatically the selected item is the same (previous). So, I need no selected item.

The ListPicker is populated from a collection. I tried according to other answers (list.SelectedIndex = -1;), but doesn't work.

My code:

        public ObservableCollection<observacao> obsObservacao { get; set; }

    public class observacao
    {
        public string descricao { get; set; }
        public double valor { get; set; }
        public string valoradicional { get; set; }

    }

 pickerPagto1.ItemsSource = obsObservacao;
            pickerPagto1.UpdateLayout();

The Problem:

        private void botaoObs_Click(object sender, RoutedEventArgs e)
    {
        testarObs = "1";
        pickerPagto1.Open();
    }

Solution

  • As others have noted the ListPicker control must always have a selected item.

    The way around this is to add a default first item to the list. You havent posted the code where you populate your collection, but this might be a good generic solution:

        protected ObservableCollection<observacao> _obsObservacao;
        public ObservableCollection<observacao> obsObservacao
        {
            get { return _obsObservacao;}
            set {
                _obsObservacao = value;
    
                observacao noSelection = new observacao();
                noSelection.descricao ="Nothing Selected";
    
                _obsObservacao.Insert(0, noSelection);
            }
        }