简体   繁体   English

绑定列表 <string> 到ComboBox

[英]Binding List<string> to ComboBox

I want my combobox item names and values to be taken from my List of course I don't want my view model to hold combobox items list. 我希望我的组合框项目名称和值可以从我的列表中获取当然我不希望我的视图模型保存组合框项目列表。

I got a list a,b,c,d 我有一个清单a,b,c,d

i want my combobox to have items 我希望我的组合框有物品

my viewmodel got 我的观点模型了

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

my view got 我的观点得到了

<ComboBox Name="cbPropName" ItemsSource="PropsList"/>

it gives me combo with P rops L ist 它给了我与P rops L ist的组合

:( :(

Nahum you'll have to use ItemsSource="{Binding PropsList}" , assuming PropsList is a Dependancy property in your code behind or, even better, a property in your data context that implements INotifyPropertyChanged . Nahum你将不得不使用ItemsSource="{Binding PropsList}" ,假设PropsList是你的代码背后的Dependancy属性,或者更好的是,数据上下文中实现INotifyPropertyChanged的属性。 In your case you'd have to set the DataContext property of your View to be the ViewModel 在您的情况下,您必须将View的DataContext属性设置为ViewModel

try this: 尝试这个:

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

also check this out: Binding WPF ComboBox to a Custom List 还要检查一下:将WPF ComboBox绑定到自定义列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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