将组合框数据从列表绑定到属性

时间:2014-12-20 16:21:24

标签: c# wpf mvvm combobox

我有一个combo box和按钮

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

MessageTypesList列表是从SQL查询生成的,一旦从列表中选择了消息类型,搜索按钮就需要将所选值传递给我的ViewMainModel中的字符串属性。

当我调试应用程序时,传递给MessageType属性的值始终为NULL。我有一个类似的日期时间搜索工作,但无法看到如何将我的XAML中的MessageType值传递给MessageType propery表单绑定生成列表。

2 个答案:

答案 0 :(得分:2)

您应该将SelectedValue属性绑定到viewmodel中的属性。

答案 1 :(得分:2)

在viewmodel中创建一个属性:

public MessageType SelectedType {get;set;}

将selectedItem绑定到XAML中的此属性:

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