none
Programatically set selected item in listbox

    Question

  • Hi

    Senario:

    I'm opening a silverlight app from an Msaccess app.  I'm passing parameters in the querystring and parsing them out in the constructor of the silverlight viewmodel - this works fine.

    I'm parsing the data and creating the same type of object thats bound to the listbox (Route object)  and also parsing a date from the querystring.

    The 'SelectedItem' of the listbox is bound to a public property in the viewmodel (Implements INotifyPropertyChanged).

    SelectedItem = "{Binding SelectedRoute, Mode=TwoWay}"

    When I create the object from the querystring I set it as the 'SelectedRoute' (If I put a breakpoint in code I can see that its set properly) yet visually on the page nothing is selected in the listbox.  I have explicity called the INotifyPropertyChanged for 'SelectedRoute' but still no luck

    I have a calander control bound to the date I'm parsing and it works as expected but can't seem to get the listbox to work.

    Has anyone got this to work?

    Thanks

    Paul

     

      

    Thursday, November 10, 2011 5:28 AM

Answers

  • Hi Paul

    1. Depending on how PropertyChanged is invoked, you can have a typo in VM calling it. Is the property name correct? There must be PropertyChanged(this, new PropertyChangedEventArgs("SelectedRoute")) called.

    2. Has the list really such an entry? if not found, nothing is selected. 

    3. Have you any other binding working in this VM? If not, maybe DataContext is not set correctly.

    Good luck, it will work!

    Thursday, November 10, 2011 9:48 AM

All replies

  • Hi Paul

    1. Depending on how PropertyChanged is invoked, you can have a typo in VM calling it. Is the property name correct? There must be PropertyChanged(this, new PropertyChangedEventArgs("SelectedRoute")) called.

    2. Has the list really such an entry? if not found, nothing is selected. 

    3. Have you any other binding working in this VM? If not, maybe DataContext is not set correctly.

    Good luck, it will work!

    Thursday, November 10, 2011 9:48 AM
  • Hi Greg

    2. Has the list really such an entry? if not found, nothing is selected.

    The above comment got me thinking - I've harcoded the data to match what's in the list and it still didn't work.

    Route object contains RouteID which is unique so what I did was as follows:

    //My object - hardcoded for now but will be passed in querystring
    Routes r = new Routes();
    r.RouteID = 36;
    r.Route = "Route 1";
    r.RouteColour = "Red";
    
    //routes in foreach is list of routes contained in listbox
    //find entry where my RouteID = RouteID in object contained in listbox
    //Set Route object found in the listbox as the 'SelectedRoute' - don't use my Route object 
    
    foreach (Routes item in routes)
    {
    if (r.RouteID == item.RouteID)
    {
    _selectedRoute = item;
    _routeSelectionList.Add(item.RouteID);
    InvokePropertyChanged("SelectedRoute");
    }
    }
    Hey presto it works!
    Thanks for the pointer Greg
     
    Thursday, November 10, 2011 10:36 AM
  • You're welcome. Glad you solved it.

    Thursday, November 10, 2011 10:47 AM
  • Thanks for the pointer, Greg -- it helped me fix a similar problem.

    Tuesday, May 01, 2012 8:56 PM