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. WCF and WF
  4. Listbox bound to ObservableCollection, Set SelectedIndex to last one

Listbox bound to ObservableCollection, Set SelectedIndex to last one

Scheduled Pinned Locked Moved WCF and WF
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.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi! I have an observablecollection which is instanced in xaml. A listbox is bound to this collection. Binding works great, no problems so far. What I'm struggling with is how to force the listbox via xaml to select the last index available when the observablecollection fires an event. e.g: ((int)2 =) oscoll.Count(); SelectedIndex = 1; obscoll.Add(new Item()); ((int)3 =) oscoll.Count(); SelectedIndex = 2; And this should be done in xaml :) Thank you in advance, eza

    A 1 Reply Last reply
    0
    • E ezazazel

      Hi! I have an observablecollection which is instanced in xaml. A listbox is bound to this collection. Binding works great, no problems so far. What I'm struggling with is how to force the listbox via xaml to select the last index available when the observablecollection fires an event. e.g: ((int)2 =) oscoll.Count(); SelectedIndex = 1; obscoll.Add(new Item()); ((int)3 =) oscoll.Count(); SelectedIndex = 2; And this should be done in xaml :) Thank you in advance, eza

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      Bind ListBox's SelectedIndex to Count property (of the ObservableCollection) . In the binding you would have to use a converter to return a Count - 1 value. Something like this,

      <ListView Name="listView" ItemsSource="{Binding}" SelectedIndex="{Binding Path=.Count, Mode=OneWay, Converter={StaticResource SelectedIndexConverter}}" >
      <ListView.ItemTemplate>
      <DataTemplate>
      <TextBox Text="{Binding Path=FirstName}"></TextBox>
      </DataTemplate>
      </ListView.ItemTemplate>
      </ListView>

      The Converter will get the current count value and it will return a Count - 1 value.

      public class SelectedIndexConverter : IValueConverter
      {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
      return (object)(System.Convert.ToInt32(value) - 1 );
      }

          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
      
              throw new Exception("The method or operation is not implemented.");
      
          }
      }
      
      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