David
David

Reputation: 4117

ObservableCollection get item on SelectionChanged

I have a WPF ComboBox:

<ComboBox ... ItemsSource="{Binding Source={StaticResource viewModel}, Path=getItems, Mode=OneTime}" x:Name="combobox" SelectionChanged="combobox_SelectionChanged">
    ...
</ComboBox>

with lots of items.

And my ViewModel class:

public class ViewModel
{
    private readonly ObservableCollection<ObjectA> _objectACollection= new ObservableCollection<ObjectA>(); 

    public ViewModel()
    { 
        _objectACollection.Add(new ObjectA("Text 1", "Text", "Text"));
        _objectACollection.Add(new ObjectA("Text 2", "Text", "Text"));
        _objectACollection.Add(new ObjectA("Text 3", "Text", "Text"));
    }

    public void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Trace.WriteLine(combobox.SelectedIndex);
    }

    public ObservableCollection<ObjectA> getItems
    {
        get { return _objectACollection; }
    }
}

and the selectionChanged listener:

private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Trace.WriteLine(combobox.SelectedIndex);
}

The ComboBox is displayed and when I choose something I get the index of the collection objects. But is there any way to return me the object? for example: I select the first element in the ComboBox(index 0), how can I get (in the combobox_SelectionChanged listener) the object from the _objectACollection with index 0?

Upvotes: 2

Views: 5214

Answers (3)

DaMightyMouse
DaMightyMouse

Reputation: 822

Maybe you could try using a collection that implements ICollectionView interface, I am sure there are a few baked in. It keeps track of the selected item in your collection for you without the need for a separate SelectedObjectA property on your viewmodel. So you can have:

> <ComboBox ... ItemsSource="{Binding Source={StaticResource viewModel},
> Path=**SomeICollectionView**, Mode=OneTime}" x:Name="combobox"> ...
> </ComboBox>

to get the selected item from the viewmodel class all you have to do is SomeICollectionView.CurrentItem

Upvotes: 0

Pongsathon.keng
Pongsathon.keng

Reputation: 1435

There is SelectedItem property of ComboBox. I think that you can bind SelectedItem with TwoWay with your VM. Following is exmaple. I hope that this help.

<ComboBox ... ItemsSource="{Binding Source={StaticResource viewModel}, Path=getItems, Mode=OneTime}" x:Name="combobox"  SelectedItem="{Binding SelectedObjectA, Mode=TwoWay}">
...
</ComboBox>

You should add SelectedObjectA property in your VM. You can get selected item from VM.SelectedObjectA property.

    private ObjectA _SelectedObjectA;
    public ObjectA SelectedObjectA
    {
        get
        {
            return _SelectedObjectA;
        }
        set
        {
            if (_SelectedObjectA == value)
                return;

            _SelectedObjectA = value;

            // Notifu changed here
        }
    }

Upvotes: 3

Dummy01
Dummy01

Reputation: 1995

You can use combobox.SelectedItem.

Upvotes: 2

Related Questions