Ross
Ross

Reputation: 325

Binding combobox data bult from a list to a property

I have a combo box and button as such

<ComboBox  ItemsSource="{Binding MessageTypesList}" 
           DisplayMemberPath="MessageType" 
           SelectedValue="MessageType" />
<Button Content="Search" 
        Command="{Binding Path=SearchMessageTypes}" 
        x:Name="SearchMessageTypeButton"/>

The MessageTypesList list is generated from a SQL query and once the Message Type is selected from the list the Search button needs to pass the selected value to a string property in my ViewMainModel.

When I debug the application the value passed to the MessageType property is always NULL. I have this working for a similar date time searchs but cant see how to pass the MessageType value in my XAML to the MessageType propery form binding generated lists.

Upvotes: 0

Views: 42

Answers (2)

opewix
opewix

Reputation: 5083

Create a property in your viewmodel:

public MessageType SelectedType {get;set;}

Bind selectedItem to this property in XAML:

<ComboBox  ItemsSource="{Binding MessageTypesList}" SelectedItem="{Binding SelectedType, Mode=TwoWay" />

Upvotes: 2

John
John

Reputation: 3702

You should bind the SelectedValue property to a property in your viewmodel.

Upvotes: 2

Related Questions