ListBox selection through code, in MVVM
-
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?
-
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?
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 areObservableCollection
s... To automatically select the first item in listbox1, simply set theSelectedIndex
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 -
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 areObservableCollection
s... To automatically select the first item in listbox1, simply set theSelectedIndex
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, 2013Ahh ... 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
-
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
"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 -
"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, 2013Thanks 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.
-
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.
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 -
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, 2013After 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 :-)
-
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 :-)
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
-
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 :-)
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