ListBox Mvvm - Hilight selected item - wpf

I've a simple listbox bound to ICollectionView.
When I execute something like MyCollection.MoveCurrentToPosition(index) I can see selected item event fired by my listbox, however I can't see any item hilighted.
How can I hilight a listbox item when CurrentItem of my collection view changes?

Related

Cannot clear selection of ListView

In my listview, when I select on of the item I will show a new view and set the listview to not select any item by set SelectedItem to be null. But the listview still select the old item that I selected. According to this link, I have set
IsSynchronizedWithCurrentItem="True"
But it still same. My item list is compose from the ItemViewModel that inherited from MvxViewModel
Are you using ListView(ListBox) or any custom control?
A class inherited from MS ListBox (such as ListView) has a static method to unselect all selected items:
ListBox.UnselectAll()
To unselect only one item you can cast selected item to ListBoxItem object and call:
ListBoxItem item = (ListBoxItem)obj;
item.IsSelected = false;
Have you tried it in your code behind? Or you want to achieve this declaratively by XAML markup?

Binding CommandParameter to code behind Property

I have a WPF View where I create controls dynamically depending on the object type in a List that the View is bound to in the ViewModel.
I have a Button on in the View that I have bound to a RelayCommand in the ViewModel but I also want to pass a CommandParameter to the Command.
The dynamically create controls are of types ListBox, ComboBox, TreeView, RadionButton and CheckBox. So when I press the Button I want to get all selected/checked items in the controls and pass this a List with the CommandParamter.
I have figured out how to search for all selected/checked items and get the object of type Code(class name) and put them in a List if I use the Click event on the Button. I want to use the RelayCommand instead of using Click Event.
I have managed to bind the CommandParameter to the a Property that calls GetAllSelectedCheckedCodes() but it is only bound when the View is created.
Is it possible to have a property in the code behind that calls the function GetAllSelectedCheckedCodes() that is bound first when the Button Command is triggered.
Or is it possible to have multiple controls add/remove items in a List in the ViewModel when they are selected/unselected or checked/unchecked?
Are you able to add a property to your class like IsSelected, or IsActive, and bind that to the the IsSelected or IsChecked property of the control? Then, when you want to act on the selected items, you could just grab all the items in the collection where IsActive == true.

Double databinding

I am just learning WPF (and mvvm), and I have encountered a problem which I can't google through.
I have 2 ObservableCollections - exercises and charts (the project is about trackig progress in a gym):
Exercise (Id, Name)
Chart (ExerciseId, ExerciseName, Id, ...)
Now in a window where I want to fill the charts, I have a listbox with some labels and a _grid_ and a combobox in ItemTemplate.
Listbox is binded to Chart collection.
Combobox shows a list of exercises, so I am binding it to Exercise collection.
Questions:
Can I specify in xaml that combobox current value should be same as Chart.Exercise?
How can I specify a binding in XAML so Exercise collection element from a combobox would be assigned to Chart.Exercise?
You could possibly achieve this with Element Binding and a Converter... However, it would be simpler to achieve this in the ViewModel.
You would have four Properties in your ViewModel.
Property 1: Exercise Observable Collection - Bound to your ComboBox ItemSource (ExerciseItems)
Property 2: Exercise Selected Item - Bound to your Combobox SelectedItem (ExerciseSelectedItem)
Property 3: Chart Observable Collection - Bound to your ListBox (ChartItems)
Property 4: Chart Selected Item - Bound to your ListBox SelectedItem (ChartSelectedItem)
You would Set your Combo Box Selected Item, using Linq perhaps, to be equal to the Item with ListBox Selected Item ExerciseID, in the Setter of the ListBox Selected Item Property;
Public Property ChartSelectedItem As ChartItem
Get
Return _ChartSelectedItem
End Get
Set(value As ChartItem)
If value <> _ChartSelectedItem Then
_ChartSelectedItem = value
ExcersiseSelectedItem = (From ExcersiseItemsList in ExcersiseItems Where ExcersiseItemsList.ID = value.ExcersiseID).FirstOrDefault
OnPropertyChanged("ChartSelectedItem")
End if
End Set
End Property
Hope that helps...

Set focus to List item on Listview

I have a Listview her ItemsSource property is binded to ViewModel, when i add a new item to List (ObservableCollection) i need set focus to the new item.
Bind the SelectedItem property of the ListView to a property in your ViewModel and when you add a new item to the collection set the property in your ViewModel to reference that new item and the binding will take care of the rest.

How Do I Change WPF Listview SelectedItem Font Color With ItemSource Bound?

I have WPF window containing a listview that has it's itemsource set to a collection of objects. When I access SelectedItem or SelectedItems[] or Items[], I get the my object back that's bound to that item, not the ListViewItem item itself. I have no idea how to select a row and change it's color since I can't access the item itself, like a winform listviewitem.
ListView derives from ItemsControl which exposes the ItemContainerGenerator property. This object allows you to map a bound entity to its ItemContainer (the item your are looking for) and back.

Resources