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. ListBox selection through code, in MVVM

ListBox selection through code, in MVVM

Scheduled Pinned Locked Moved WPF
wpfarchitecturequestion
9 Posts 3 Posters 7 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.
  • A Offline
    A Offline
    Amarnath S
    wrote on last edited by
    #1

    I have two ListBoxes on the screen, and this is my requirement:- Upon loading the window, the first listbox (ListBox1) should be populated with items (which is happening correctly), but I also want the first item to be selected. This is to happen via MVVM, and not through codebehind. And upon selecting in the first listbox (ListBox1), the second listbox (ListBox2) should be populated (which is happening correctly), and its first item (of ListBox2) is to be selected. How is this listbox selection to be achieved via MVVM?

    realJSOPR 1 Reply Last reply
    0
    • A Amarnath S

      I have two ListBoxes on the screen, and this is my requirement:- Upon loading the window, the first listbox (ListBox1) should be populated with items (which is happening correctly), but I also want the first item to be selected. This is to happen via MVVM, and not through codebehind. And upon selecting in the first listbox (ListBox1), the second listbox (ListBox2) should be populated (which is happening correctly), and its first item (of ListBox2) is to be selected. How is this listbox selection to be achieved via MVVM?

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      Your listbox1 needs to handle the SelectionChanged event, which when executed, populates the collection for listbox2. Since listbox2 is bound to the appropriate collection, it will automatically update itself. You didn't say whether or not you wanted to retain the selected item from listbox1 in the viewmodel. I'm assuming that your bound collections for both list boxes are ObservableCollections... To automatically select the first item in listbox1, simply set the SelectedIndex property to 0.

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      L 1 Reply Last reply
      0
      • realJSOPR realJSOP

        Your listbox1 needs to handle the SelectionChanged event, which when executed, populates the collection for listbox2. Since listbox2 is bound to the appropriate collection, it will automatically update itself. You didn't say whether or not you wanted to retain the selected item from listbox1 in the viewmodel. I'm assuming that your bound collections for both list boxes are ObservableCollections... To automatically select the first item in listbox1, simply set the SelectedIndex property to 0.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

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

        Ahh ... but you didn't say "where" to set index to 0. He wants "no code behind". I guess you can set it in XAML, but isn't it "0" anyway? (Actually, I thinks it's -1 when newly loaded. Shrug) Just taking digs at "no code behind" MVVM; not your answer. It's "plumbing". To say you can't put it in a loaded event (as "code") is OCD, IMO.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        realJSOPR 1 Reply Last reply
        0
        • L Lost User

          Ahh ... but you didn't say "where" to set index to 0. He wants "no code behind". I guess you can set it in XAML, but isn't it "0" anyway? (Actually, I thinks it's -1 when newly loaded. Shrug) Just taking digs at "no code behind" MVVM; not your answer. It's "plumbing". To say you can't put it in a loaded event (as "code") is OCD, IMO.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          "No code behind" is a pointless endeavour, and a highly artificial requirement. It also means that he app is virtually impossible (or at least a pain in the ass) to debug. I make it a practice to put as much as possible in the code behind for this very reason. Sure, control interaction can be put into the XAML many times, but I pretty much use code-behind for everything. In the real world, you do what works, and use the simplest means possible. Doing the work is no time to exercise classroom theory or hypotheticals. Experienced programmers (like us) are cognizant of these facts and are much more efficient coders as a result.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          A 1 Reply Last reply
          0
          • realJSOPR realJSOP

            "No code behind" is a pointless endeavour, and a highly artificial requirement. It also means that he app is virtually impossible (or at least a pain in the ass) to debug. I make it a practice to put as much as possible in the code behind for this very reason. Sure, control interaction can be put into the XAML many times, but I pretty much use code-behind for everything. In the real world, you do what works, and use the simplest means possible. Doing the work is no time to exercise classroom theory or hypotheticals. Experienced programmers (like us) are cognizant of these facts and are much more efficient coders as a result.

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            A Offline
            A Offline
            Amarnath S
            wrote on last edited by
            #5

            Thanks for your replies. In fact, I have a fully working Codebehind solution, which is quite less code, and is working perfectly well. I was asked by my manager to convert it to MVVM, and am facing somewhat unsurmountable difficulties, in making it non-codebehind. That was the reason for my question. Now, having listened to the opinion of experts like you, I will convince my manager that some codebehind is not taboo.

            realJSOPR 1 Reply Last reply
            0
            • A Amarnath S

              Thanks for your replies. In fact, I have a fully working Codebehind solution, which is quite less code, and is working perfectly well. I was asked by my manager to convert it to MVVM, and am facing somewhat unsurmountable difficulties, in making it non-codebehind. That was the reason for my question. Now, having listened to the opinion of experts like you, I will convince my manager that some codebehind is not taboo.

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              To be clear, MVVM does not mean no code-behind. It’s about separation of concerns. The model loads and saves the data. The view model both transforms the model for, and eases interaction with the view. The view binds the view model to the ui. MVVM can be implemented in such a way as to couple the three concerns as loosely or as tightly as is necessary.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

              A 1 Reply Last reply
              0
              • realJSOPR realJSOP

                To be clear, MVVM does not mean no code-behind. It’s about separation of concerns. The model loads and saves the data. The view model both transforms the model for, and eases interaction with the view. The view binds the view model to the ui. MVVM can be implemented in such a way as to couple the three concerns as loosely or as tightly as is necessary.

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                A Offline
                A Offline
                Amarnath S
                wrote on last edited by
                #7

                After yesterday's demo, the requirement got changed. Instead of two listboxes, one listbox and one datagrid is what is needed. So, I am off to understanding datagrid programming now. The weird software world we are all in :-)

                L realJSOPR 2 Replies Last reply
                0
                • A Amarnath S

                  After yesterday's demo, the requirement got changed. Instead of two listboxes, one listbox and one datagrid is what is needed. So, I am off to understanding datagrid programming now. The weird software world we are all in :-)

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

                  There's also an inclination to get carried away with "data grids". They're useful for browsing but they'll suck up all your development time if you (try to) use them for data entry instead of using a "form" (for the data entry / update part).

                  It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                  1 Reply Last reply
                  0
                  • A Amarnath S

                    After yesterday's demo, the requirement got changed. Instead of two listboxes, one listbox and one datagrid is what is needed. So, I am off to understanding datagrid programming now. The weird software world we are all in :-)

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #9

                    I use ListViews. In fact. I wrote an article on CodeProject that illustrates a custom ListView control that can auto-generate columns based on the dataset being represented. It doesn't support editable cells, but it does have sort functionality (that can be toggled on a column-by-column basis, column name overrides, custom column formatting, and other stuff. Auto-generated columns in a WPF ListView[^]

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                    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