简体   繁体   中英

WPF ComboBox to ObservableCollection binding

I know there is a few topics on this problem but haven't find any solutions to this problem I have..

I have a ViewModel with an observable collection and I want to bind this collection to a combo box. However, there is no selected item, no index, just the collection itself.

in the XAML I have

ComboBox ItemsSource="{Binding OSCollection}" DisplayMemberPath="OSCollection.Name"

I believe the trouble lies with the bold above, I want to get a property from the collection called name, but like I say - no item will be selected before the bind.

I could use a foreach or something to extract the properties from the collection but I don't think this is the MVVM and WPF way.

Any help would be grateful.

Thanks

DisplayMemberPath指定显示属性的路径。因此它应该是Name而不是OSCollection.Name

  ComboBox ItemsSource="{Binding OSCollection}" DisplayMemberPath="Name"

In addition to Sajeetharans comment: When binding to a List of Type T , DisplayMemberPath will always refer to the Name of a Property of T . In your case it is only "Name"

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