Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Selector (ListBox, ComboBox) default selected item woes when bound to ObservableCollection

Selector (ListBox, ComboBox) default selected item woes when bound to ObservableCollection

Scheduled Pinned Locked Moved WPF
csharpdatabasecomhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Likness
    wrote on last edited by
    #1

    OK, here is the scenario. Take a selector (any type of list item that you can select values) and bind it to an observable collection. Let's say that collection is populated from a service call. The issue is that you bind, the collection is empty, so there is no selected index. You cannot plug into the Loaded event on the box because if you try to set selectedindex = 0, and the collection hasn't loaded yet, you're out of luck. In the code behind I can wire into the collectionchanged and then set selectedindex = 0 but that seems a little contrived. I have a full solution using attached properties but it seems overly complicated for the behavior (I want to always default to the first item in a collection when the collection becomes available). Anyone have a similiar scenario and thoughts about a solution? One thing I looked at was having the view model raise a "collection loaded" event, but then again the view will have to hook into that in the code behind. My attached properties solution basically creates a list of weak references between the selectors and the collections, then hooks into the collection changed event. When it fires, it finds the selector it is linked to and then sets the selected index. It works like a charm but I want to make sure I'm not overcomplicating it when there may be some setting that says, "Default to the first item" when a collection gets filled. Thanks, Jeremy

    Jeremy Likness Latest Article: Silverlight Behaviors and Triggers: TextBox Magic Blog: C#er : IMage

    N 1 Reply Last reply
    0
    • J Jeremy Likness

      OK, here is the scenario. Take a selector (any type of list item that you can select values) and bind it to an observable collection. Let's say that collection is populated from a service call. The issue is that you bind, the collection is empty, so there is no selected index. You cannot plug into the Loaded event on the box because if you try to set selectedindex = 0, and the collection hasn't loaded yet, you're out of luck. In the code behind I can wire into the collectionchanged and then set selectedindex = 0 but that seems a little contrived. I have a full solution using attached properties but it seems overly complicated for the behavior (I want to always default to the first item in a collection when the collection becomes available). Anyone have a similiar scenario and thoughts about a solution? One thing I looked at was having the view model raise a "collection loaded" event, but then again the view will have to hook into that in the code behind. My attached properties solution basically creates a list of weak references between the selectors and the collections, then hooks into the collection changed event. When it fires, it finds the selector it is linked to and then sets the selected index. It works like a charm but I want to make sure I'm not overcomplicating it when there may be some setting that says, "Default to the first item" when a collection gets filled. Thanks, Jeremy

      Jeremy Likness Latest Article: Silverlight Behaviors and Triggers: TextBox Magic Blog: C#er : IMage

      N Offline
      N Offline
      Nigel Ferrissey
      wrote on last edited by
      #2

      Hi Jeremy, One way could be to have a property in the ViewModel like this:

      private int _selectedIndex = -1;
      public int SelectedIndex
      {
      get { return _selectedIndex; }
      set
      {
      if (value != _selectedIndex)
      {
      _selectedIndex = value;
      NotifyPropertyChanged("SelectedIndex");
      }
      }
      }

      It's set to -1 initially so the XAML doesn't throw an error when there's no collection to bind to. Bind it to the list box:

      <ListBox ItemsSource="{Binding Employees}" SelectedIndex="{Binding SelectedIndex}" />

      Then, in the completed event (when the service returns from the asynchronous call with the data) you can set it to whatever you want.

      void svcClient_GetDataCompleted(object sender, EmployeeSvc.GetDataCompletedEventArgs e)
      {
      Employees = e.Result;
      SelectedIndex = 1;
      }

      Not particularly elegant, but it seems to work. Cheers.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups