简体   繁体   中英

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.

您应该将SelectedValue属性绑定到视图模型中的属性。

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM