1

I'd like to get the selected Item of a ComboBox using the MVVM Pattern (beginner). I've read that this could be achieved by binding the SelectedItem Property to a Property in the ViewModel.

XAML:

<ComboBox ItemsSource="{Binding RoomLockerLinkCollection}" 
        DisplayMemberPath="Room.Name" 
        SelectedItem="{Binding SelectedRoom}"/>

ViewModel:

public Room SelectedRoom { get; set; }

But it's not working - the only thing thats happening is a appearing red border around that ComboBox - in addition after selecting a new item in the ComboBox the "SelectedRoom" property in my VM is still null.

Edit 1 :

One short additional question:

The binding works fine - at least for the top "category". My Wrapper-Class also contains a list of lockers.

<ComboBox DataContext="{Binding SelectedItem, ElementName=_cmbRoomSelection}" ItemsSource="  {Binding LockerCollection}" DisplayMemberPath="Name" SelectedValue="{Binding SAVM.SelectedLocker, Mode=TwoWay}" />

When I check the type of the SelectedValue it's a "Locker" - fine. But the SelectedLocker-Property in my VM stays null...

Additional, could s.o. explain when to use "SelectedItem" and "SelectedValue"? What's the difference? Setting the DataContext in the xaml code above can not be done by binding the SelectedValue...

Edit 2 (Solution) :

Okay, got it!

As I figured out I've reset my DataContext - now the Property SAVM of course could not be found.

Solution:

<ComboBox DataContext="{Binding SelectedItem, ElementName=_cmbRoomSelection}" 
ItemsSource="{Binding LockerCollection}" 
DisplayMemberPath="Name" 
SelectedValue="{Binding SAVM.SelectedLocker **ElementName=_vStorage**, Mode=TwoWay}" />
Th1sD0t
  • 1,089
  • 3
  • 11
  • 37
  • The red box indicates a validation error , your property and your ItemsSource might not be of the same type. – eran otzap Jan 15 '15 at 06:37
  • omg you are so right - after setting the type of the property to object to check which type I'm using I realised that I'm using a wrapper for this... Is there a way to link a property of the selecteditem to the property in my mv? e.g. my combobox is holding objects of type roomLockerLink which is containing a room and a List of Lockers - but I#d like to extract that containing room to save it in my mv-property. Hope so gets what I want to say... – Th1sD0t Jan 15 '15 at 06:43
  • Yes , one sec i'll write an answer . In the mean while read about SelectValue and SelectedValuePath – eran otzap Jan 15 '15 at 07:06
  • post LockerCollection and the SelectedLocker Properties . further more , search for how to detect binding errors in wpf . The reasons for why something isn't updated via binding are very easy to detect using the right tool . Snoop , or visual studio's output window. – eran otzap Jan 15 '15 at 08:48

1 Answers1

2

The red box is an indication of a validation error from your Binding , The most common error would be that the BindingSource and the BindingTarget are not of the same type.

Use SelectedValue and SelectedValuePath to bind to your Room object.

CS :

public class Room
{
    public string RoomName { get; set; }
}

public class RoomWrapper
{
    public Room Room { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this; 
    }


    public List<RoomWrapper> RoomWrappers
    {
        get
        {
            var list = new List<RoomWrapper>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new RoomWrapper { Room = new Room { RoomName = "Room " + i } });    
            }

            return list;
        }
    }

    private Room selectedRoom;
    public Room SelectedRoom
    {
        get { return selectedRoom; }
        set
        {
            selectedRoom = value;
        }
    }

XAML :

    <ComboBox ItemsSource="{Binding RoomWrappers}" 
           DisplayMemberPath="Room.RoomName"
           SelectedValuePath="Room" 
           SelectedValue="{Binding SelectedRoom, Mode=TwoWay}" />
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • A very big thank you. Could you explain why to use a two way binding? Cause I just want to get a selectedvalue wouldnt do a "OneWayToSource" do the job? – Th1sD0t Jan 15 '15 at 08:11
  • @Chill-X it would... But this would give you the option of setting it from your code. – eran otzap Oct 21 '15 at 06:33
  • TwoWay binding is from the user interface to the bound property and from the bound property or object to the user interface. Please review this for more information;https://stackoverflow.com/questions/2305179/what-are-the-various-wpf-binding-modes. – Fran Martinez Jun 23 '17 at 18:52