msfanboy
msfanboy

Reputation: 5291

How to set an item as selected in a combobox

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?

Upvotes: 13

Views: 27037

Answers (3)

Hazem Elamir
Hazem Elamir

Reputation: 51

what i found is that in combobox soure code, selecteditem is set by using list selectedindex combobox using

public object SelectedItem {
        get {
            int index = SelectedIndex;
            return (index == -1) ? null : Items[index];
        }
        set {
            int x = -1;

            if (itemsCollection != null) {
                //bug (82115)
                if (value != null)
                    x = itemsCollection.IndexOf(value);
                else
                    SelectedIndex = -1;
            }

            if (x != -1) {
                SelectedIndex = x;
            }
        }
    }

this method always return -1 or null every time you set Selecteditem by code x = itemsCollection.IndexOf(value); its reported as bug (82115) in combobox code

so the working method is to use SelectedIndex directly and bind to it instead of SelectemItem property and if you want you can read only the item from binding to the SelectedItem property or obtain it in your code using ItemsSource itself.

This working for me fine.

Upvotes: 0

Agies
Agies

Reputation: 1105

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");
        }
    }
}

Upvotes: 16

Wallstreet Programmer
Wallstreet Programmer

Reputation: 9677

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>();
    }
}

Upvotes: 8

Related Questions