How to set an item as selected in a combobox
25,480
Solution 1
Our successful approach for binding a combobox is the following...
<ComboBox
ItemsSource="{Binding Path=AllItems}"
SelectedItem="{Binding Path=CurrentItem, Mode=TwoWay}" />
<TextBlock Text="{Binding Path=CurrentItem, Mode=TwoWay}" />
class public ItemListViewModel
{
public ObservableCollection<Item> AllItems {get; set;}
private Item _currentItem;
public Item CurrentItem
{
get { return _currentItem; }
set
{
if (_currentItem == value) return;
_currentItem = value;
RaisePropertyChanged("CurrentItem");
}
}
}
Solution 2
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:
<Window x:Class="CBTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<ComboBox
ItemsSource="{Binding Path=Names}"
IsSynchronizedWithCurrentItem="True">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="{Binding Path=Names.CurrentItem}" />
</StackPanel>
</Window>
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<string> _namesModel = new List<string>();
}
}
Author by
msfanboy
“Example isn't another way to teach, it is the only way to teach” Albert Einstein
Updated on July 05, 2022Comments
-
msfanboy almost 2 years
It seems nobody has yet found a way to set the comboboxitem as selected with a SelectedItem="Binding Property".
Is the solution to use a IsSelected Property in the ViewModel object within the combobox itemssource?
-
msfanboy almost 14 years"...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<T> as I need it for add/del which CollectionView not has.
-
kumar TN almost 14 yearsA 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.
-
msfanboy almost 14 yearsI 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 almost 14 yearsthis 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;
-
Rasmus Christensen over 12 yearsJust 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