×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    ..., Earth, Brazil, Paraiba, Campina Grande
    Posts
    72

    Select ListBox item programatically

    Hi All,

    I'm trying to select an item of a ListBox programatically. I'm using the below code in the OnNavigatedTo method of my Page and the PlaceListOnPage is the ListBox itself. I have tried a lot of things, but the PlaceListOnPage.SelectedItem property is always null after the attribution.

    I also tried to set the SelectedIndex and SelectedValue properties, but it didn't work too.

    Anyone could help me?

    Thanks in advance

    Code:
                List<Place> allPlaces = database.GetAllPlaces();
                PlaceListOnPage.ItemsSource = allPlaces;
                
                if (allPlaces.Count > 0)
                {
                    PlaceListOnPage.SelectedItem = PlaceListOnPage.Items[0];
                }

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    568

    Re: Select ListBox item programatically

    Have you tried this with the ListBox and LongListSelector?

    The issue I find with most controls is that once you assign data (eg. PlaceListOnPage.ItemsSource = allPlaces), the control is not immediately ready (or Loaded) which is why SelectedItem might not work.

    You could try subscribing to the Loaded event, then try SelectedItem, then unsubscribe from the event. And there's also the BringIntoView method.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Location
    ..., Earth, Brazil, Paraiba, Campina Grande
    Posts
    72

    Re: Select ListBox item programatically

    Hi theothernt,

    Really thanks! It worked like a charm!

    Just to keep saved, below is my final code.

    Code:
                allPlaces = database.GetAllPlaces();
                PlaceListOnPage.Loaded += PlaceListOnPage_Loaded;
                PlaceListOnPage.ItemsSource = allPlaces;
            }
    
            void PlaceListOnPage_Loaded(object sender, RoutedEventArgs e)
            {
                if (PlaceListOnPage.Items.Count > 0)
                {
                    if (editableAlarm.Place != null)
                    {
                        PlaceListOnPage.SelectedValue = allPlaces.Find(new Predicate<Place>((place) => { return place.Id == editableAlarm.AlarmPlaceId; })); ;
                    }
                    else
                    {
                        PlaceListOnPage.SelectedIndex = 0;
                    }
                }
                PlaceListOnPage.Loaded -= PlaceListOnPage_Loaded;
            }

Similar Threads

  1. How to select 1 of the 5th edition keyPads programatically?
    By rohanwaugh in forum Symbian User Interface
    Replies: 0
    Last Post: 2008-12-18, 11:12
  2. D211 WLAN and 802.1X select connection programatically
    By seiyafujii in forum Multimodecards
    Replies: 1
    Last Post: 2003-04-30, 11:08
  3. select item in listbox
    By elenabonetti in forum Symbian
    Replies: 0
    Last Post: 2003-02-12, 07:44
  4. Select Item in ListBox
    By Nokia_Archive in forum Symbian User Interface
    Replies: 1
    Last Post: 2002-05-29, 14:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×