将List <string>绑定到ComboBox </string>

时间:2011-09-07 06:12:45

标签: c# wpf binding mvvm

我希望我的组合框项目名称和值可以从我的列表中获取 当然我不希望我的视图模型保存组合框项目列表。

我有一个列表a,b,c,d

我希望我的组合框有物品                    

我的viewmodel

public List<string> PropsList { get; set; }

我的观点得到了

<ComboBox Name="cbPropName" ItemsSource="PropsList"/>
它给了我 与P r o p s L i s t组合

:(

2 个答案:

答案 0 :(得分:13)

Nahum你必须使用ItemsSource="{Binding PropsList}",假设PropsList是代码中的Dependancy属性,或者更好的是,数据上下文中实现INotifyPropertyChanged的属性。在您的情况下,您必须将视图的DataContext属性设置为ViewModel

答案 1 :(得分:2)

试试这个:

 <ComboBox Name="cbPropName" ItemsSource="{Binding Path=PropsList}" />

也请查看:Binding WPF ComboBox to a Custom List