Multiselect ListBox - wpf

I'm having trouble in multiple items selection in an ListBox.
I've tried deriving new control from Selector and writing ListBox helper class which did not work (as expected).
The issue with Selector class is, it does not expose SelectedItems and it's hell to bind the property and manipulate it with selection changed event.
The issue with ListBox Helper class is, I'm getting the required data on multiple selection but it never hits the bound property.
Does anybody know a better way to implement multiselect listbox?
Thanks in advance...

The ListBox has multiple selection already implemented. Just change SelectionMode property to Multiple or Extened.
You can use SelectedItems property to get all the selected items afterwards.

Related

WPF MVVM DataGrid ItemsSource binding to different collection

I have a Window with a dynamic menu and a DataGrid that shows different records based on the which menu item has been clicked.
Each menu item returns an ObservableCollection of a custom class.
All the classes are diffent and not necessarily related to each other.
At the moment I created one ObservableCollection(Of Object) in my ViewModel, filling it with new results everytime a menu item is clicked.
The problem is that Object does not implement INotifyPropertyChanged, forcing me to manually assign the ItemsSource.
I'm sure there is a way to accomplish what I'm after, but I cannot think of it.
You have two options:
clear the ObservableCollection, then add the new items to it. This can be slow though as there are multiple notifications (events) fired when you do it, if it's any more than a few tens of items then you'll start to notice some UI slow downs while you do it
ensure that your viewmodel implements INotifyPropertyChanged and the property that contains the ObservableCollection fires a property change notification. Then simply assign a new ObservableCollection when you have a new list to show.
Because your collections contain different types of objects, I trust you've looked into Data Templating (another example) to structure your UI - that way you can have a custom layout that is dependent on the type of the list item.

Setting Focus to a Control after the Loading of the DataGrid

I have a UserControl in WPF and it contains a variety of controls in it... The most important being the DataGrid Control. The DataGrid control is bound to an Observable Collection list. This list filled with different items based on couple of filters selected by the user. Now, once the DataGrid displays all the data. I want to set the Focus on the first (Filters) combobox on my usercontrol. Is there any way to know when the DataGrid has completely loaded??? Is there any DataTrigger I could set that i could trigger to inform me that Grid was refreshed with new list and I can set the ComboBox to focus. Basically i'm not able to set the focus to the first control on my UserControl when the data in the DataGrid has been repopulated... Please let me know if anyone knows how to resolve this issue!!
Thanks in advance.
I hate to work in code behind but sometimes that is only way. This can be done in two ways according to me:
Use EventAggregator to raise the event from your ViewModel once you are done with your filtering logic in it. Listen to this event in view code behind and in the handler do firstCombo.Focus()
Second is bit dirty, So there must be the button or the last control after which you apply filter on your listview. In the buttonClickHandler/or any event of the last control (like selectionchange) directly do firstCombo.Focus(). In this case the moment you will press your button focus will move to combo.
Thanks

which event fires when a listbox finished databinding?

The ASP.NET GridView (and other controls) has the very handy DataBound event, which fires after the GridView finishes databinding. Is there an equivalent event for the Silverlight ListBox (WP7.1)?
My ListBox changes constantly based on user input and I would like to scroll the listbox to a certain item.
As far as I know there is no such event.
But you could use a more generic change listener like ItemsChanged:
listBox1.ItemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);
This will also react on normal list changes but you can easily filter these.
The ListBox has a SelectedItem property. Just set it to one of the items, or set the SelectedValue and that will automatically scroll it to make visible.
Here is the MSDN reference of that property: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem.aspx

How can I set a datatrigger to select\unselect listbox items?

This issue has been plaguing me for a while, and I just can't get it right. I have multiple listboxes each bound to different lists. In the end, I just want to be able to keep it so only one item is selected at a time across all the lists. Any ideas?
This has got to be very simple, but I have had no luck. I tried the idea of a gloabl\static DependencyObject to match against each ListBoxItem's Tag property, but have trouble getting the datatrigger to fire for each listbox when the value is updated.
For example, if my form has different ListBoxes, each with their own backing list collections and their own DataTemplates, I want to be able to select an item from one list, and have it be the only selected item. Which means I want the others to unselect. I tried binding the ListBoxItem IsSelected Property to a MultiBinding that compares the unique ListBoxItem value to a global value, but had no luck there.
As far as I understand, IsSynchronizedWithCurrentItem works for lists using the same backing collection.
ok, here's a try:
get the collectionviewsource.View of each of your lists (if you don't use these already, get the default views). In the View you can subscribe to CurrentChanged and in there for each other lists' CollectionViewSource.View.MoveCurrentToPosition(-1);

getting selected checkbox items from listbox mvvm

I have listbox in which I am dynamically showing the pre selected checkboxes from the database. Now I want to loop through it to get the selected items/values. I am using mvvm light wpf.
kindly Suggest?
Thanks
If you are using MVVM, then you should have the 'IsChecked' value of the checkboxes bound to some property in your view model. That property should be accessible through code and you just need to loop over the items in your model collection.
More information about your specific ViewModel would be useful.

Resources