getting selected checkbox items from listbox mvvm - wpf

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.

Related

Change the selected item of a treeview in WPF

I have a treeview with several items in it. How do I force the treeView to select a specific item ? Everytime I try to use any of the "Selected..." property, I get the error that it is read-only and cannot be set either in code or in XAML.
Regards,
You need to create a Selected property on your tree item's ViewModel, and bind to it.
Here is an article that explains how to do it.
Edit. Actually that uses a different way. The article I was thinking of was this one

Multiselect ListBox

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.

How to implement an editable listview in MVVM model?

I have an mvvm application...I need to have an editable listview.
I am binding my observable collection to listview.
How could I track changes of the values in listview ?...I mean if user edit an item...I need to have changed values in my observable collection.
If I use datagrid in WPFToolKit, is it easy ?
In a word, yes.
Take a look at data templates in WPF. They allow you to define how you want each item in your list (or any control) to appear and behave. So each item in your listview can one or more editable controls that are bound to each item in your collection (in this case an ObservableCollection). As you change data in the listview, the bound objects will in your collection will be updated in real time.
This is also possible with a datagrid.
Have a look at this link
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
It is recommended you to use the Datagrid.It already provides the edit mode functionality. You can use a TemplateColumn to provide editing views.
if you have an editable Collection in your viewmodel, just take a datagrid(editable stuff built in). you can create styles or use templates so that the datagrid look the way you want.
If I use datagrid in WPFToolKit, is it easy ?
yes ;) but if you can, use the .net4 datagrid

WPF and treeview: tricks with Items

I have some questions about treeview in WPF.
How can I change header of selected item?
How can I add child Items to selected item?
Use MVVM. In your HierarchicalDataTemplate, bind Header and ItemsSource to properties in your view model which implement change notification. Now any changes you make to those properties will be reflected in the TreeView.
If you're not using MVVM and data binding to populate the TreeView, you're wasting a lot of effort.
Robert is right, you should use MVVM and data binding. The tutorial that IMHO explained it best is here

Silverlight AutoCompleteBox SelectedValue(?)

I need to implement an editable combobox where users can select existing values from the data/tables. It needs to be editable because users can also add new rows to the table by entering new values in the editable combobox, so I put an AutoCompleteBox control into my page but I can't find any sample on how to implement such feature. It should display something like Employee Name in the editable dropdown while having the SelectedValue property to contain the Employee ID.
Any help will be very much appreciated.
Cheers!
You will need to bind your autocompletebox's ItemsSource to your "lookup" collection.
You can use ValueMemberBinding to resolve the textual input to look for, ie if you have a list of people, bind this to Model.Name like this {Binding Name} to find people by name.
As far as the drop down items, you could use templating to display the items the way you like.
Heres a good tut on the subject, you want to style the ItemTemplate. following from the example you would make a datatemplate in xaml within the ItemsTemplate element add a Textblock and bind its Text property to Name like {Binding Name}.
Here a nice example where a autocompletebox is styled like a combobox. You could extend that to look for "enter" on TextChanged and check to see if the item is contained in the ItemsSource. If not it could push the new value to the server (if you are into MVVM, you could raise a command on you ViewModel that would delegate the addition to the server and update the Items).
Here's another example that extends the AutoCompleteBox to be used as a type-ahead ComboBox. It can handle foreign keys / lookup ids using DPs and can be used in MVVM scenarios.
AutoComplete ComboBox for Silverlight

Resources