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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. WPF Listbox items

WPF Listbox items

Scheduled Pinned Locked Moved WPF
csharpdatabasewpfhelpquestion
3 Posts 3 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.
  • P Offline
    P Offline
    programmervb netc
    wrote on last edited by
    #1

    First of all I have a listbox in which I am using a data template to show a stackpanel with a textbox and buttons(one for each day of the week).. The question I have is because these stackpanels are added dynamically to the listbox is there some way that I can know the index of the item in the listbox from which a button is clicked. Using the button click event I don't see a way that I can tell if btnSunday is from row 0 or row 1. Any help would be appreciated thank you. Humble Programmer

    L D 2 Replies Last reply
    0
    • P programmervb netc

      First of all I have a listbox in which I am using a data template to show a stackpanel with a textbox and buttons(one for each day of the week).. The question I have is because these stackpanels are added dynamically to the listbox is there some way that I can know the index of the item in the listbox from which a button is clicked. Using the button click event I don't see a way that I can tell if btnSunday is from row 0 or row 1. Any help would be appreciated thank you. Humble Programmer

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Rather than using the Button click event you can use RelayCommand with in the object in your collection. You then can bubble the command to the owning data context which can check its collection for the index (if the index is really what you needed).

      private RelayCommand _someCommand;
      public ICommand SomeCommand
      {
      if(_someCommand == null)
      {
      _someCommand = new RelayCommand(() => YourDesiredAction());
      }
      }

      ...
      private void YourDesiredAction()
      {
      //Usually the action you need to run is here in this object, but if not raise an event
      //for the owner to catch and trigger what it needs to do
      RaiseSomeEvent();
      }

      ...

      public event EventHandler SomeEvent;
      private RaiseSomeEvent()
      {
      var handler = SomeEvent;
      if(handler != null)
      {
      SomeEvent(this, EventArgs.Empty);
      }
      }

      Then in your owning object you can receive it and get the index (if really need be... I have said this a few times because my guess is you do not really need the index, but need to access the object. But I am not certain as you have not really explained what you are trying to do).

      private void SomeEventHander(object sender, EventArgs args)
      {
      var index = Collection.IndexOf(sender);
      }

      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

      1 Reply Last reply
      0
      • P programmervb netc

        First of all I have a listbox in which I am using a data template to show a stackpanel with a textbox and buttons(one for each day of the week).. The question I have is because these stackpanels are added dynamically to the listbox is there some way that I can know the index of the item in the listbox from which a button is clicked. Using the button click event I don't see a way that I can tell if btnSunday is from row 0 or row 1. Any help would be appreciated thank you. Humble Programmer

        D Offline
        D Offline
        Dean Oliver
        wrote on last edited by
        #3

        First of all out the box WPF wraps the listbox control in what is called a CollectionViewSource. This can provide you with information on the currently selectedItem as well as sorting and grouping items. Get the CurrentItem and on your ObservableCollection of buttons just call IndexOf() and pass in the item. This should give you your index. for exampe: In your view xaml on the listbox set

        IsSynchronizedWithCurrentItem="True"

        then in your viewmodel (just bare inmind my collection is of themes)

        public MainViewModel()
        {
        _theme = new ObservableCollection<Themes>();

              \_themesView = CollectionViewSource.GetDefaultView(\_theme);
              \_themesView.CurrentChanging += new CurrentChangingEventHandler(\_themesView\_CurrentChanging);
              \_themesView.CurrentChanged += new EventHandler(\_themesView\_CurrentChanged);
          }
        

        This allows you to than have access to the current item of the collection. In the event below you can do whatever you want with the current object. eg; index of your button item that you have selected.

        void _themesView_CurrentChanged(object sender, EventArgs e)
        {
        ListCollectionView view = sender as ListCollectionView;
        int index = _theme.IndexOf(view.CurrentItem as Themes);
        }

        Hope this helps to some extent.

        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