MVVM,WPF: How to set a item as selected in a combobox?

Our successful approach for binding a combobox is the following.

Our successful approach for binding a combobox is the following... class public ItemListViewModel { public ObservableCollection {get; set;} private Item _currentItem; public Item CurrentItem { get { return _currentItem; } set { if (_currentItem == value) return; _currentItem = value; RaisePropertyChanged("CurrentItem"); } } }.

1 this is weird. I could swear I did exactly what you suggested before because I read about it on some blogs... Now I tried again and it worked :P In the meantime I helped with this if someone is interested XD // Set the new created Schoolclass as selected index in the UI control .. SelectedSchoolclassIndex = (Schoolclasses. Count!

= 0)? Schoolclasses. Count - 1 : 0; – msfanboy May 22 '10 at 10:04 Just had this problem.

I had 2 seperate collections, and forgot the equals operater so the currentitem was picked from another collection than the one I binded to from XAML. So implementing equals fixed the issue. But selecting from the same collection also fixed the issue – Rasmus Christensen Sep 7 at 11:04.

Not sure why you can't data bind to SelectedItem on a ComboBox without seeing your code. Below shows you how to do it using a CollectionView which has current item management built in which comboboxes supports. CollectionView has a CurrentItem get property you can use to get currently selected.

XAML: Code behind: using System.Collections. Generic; using System. Windows; using System.Windows.

Data; namespace CBTest { public partial class Window1 : Window { public Window1() { InitializeComponent(); DataContext = new VM(); } } public class VM { public VM() { _namesModel. Add("Bob"); _namesModel. Add("Joe"); _namesModel.

Add("Sally"); _namesModel. Add("Lucy"); Names = new CollectionView(_namesModel); // Set currently selected item to Sally. Names.

MoveCurrentTo("Sally"); } public CollectionView Names { get; private set; } private List _namesModel = new List(); } }.

...Not sure why you can't data bind to SelectedItem on a ComboBox without seeing your code...." do a easy google its a very wide spreaded problem. CollectionView is total overhead in my case furthermore I can and will not kick my ObservableCollection as I need it for add/del which CollectionView not has. – msfanboy May 18 '10 at 19:46 A CollectionView can be a view of an ObservableCollection so need to kick anything.

What do you mean that CollectionView is total overhead? Are you talking about the other functionality that a CollectionView provides beside Current, like filtering, grouping and sorting? I still don't know what's the problem binding selecteditem on a combobox.

– Wallstreet Programmer May 18 '10 at 20:51 I do not need a CollectionView thats it ;-) If I want to sort thats the job of the control, in my case the DataGrid has this functionality. CollectionView is okish for a listview which does not sort clicking on the column header. – msfanboy May 19 '10 at 19:09 dude, thx for the collectionview sample, it is usefull in another scenario :) – msfanboy Jun 9 '10 at 21:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions