Map object properties to comboxbox in WPF mvvm
我正在尝试将列表绑定到WPF中的组合框,但没有得到我期望的/需要的。
这是我的
中的内容
1 | public ICollection<FileType> FileTypes { get; private set; } |
这是我的FileType类:
1 2 3 4 5 6 | public class FileType { public string Description { get; set; } public string Code { get; set; } public ICollection<FileAction> FileActions { get; set; } } |
这就是我绑定我的ComboBox的方式:
1 2 3 | <ComboBox Name="uxFileTypeBox" Grid.Row="1" Grid.Column="1" Margin="10,10,10,10" Grid.ColumnSpan="2" ItemsSource="{Binding FileTypes}" SelectedItem="{Binding SelectedFileType}"/> |
ComboBox显示对象而不是其Description,我如何让ComboBox显示Description属性,并且如果可能,请使用Code属性作为值。
注意:我正在使用Toolkit MVVM Light
您的ItemsSource绑定正确,请删除SelectedItem属性,然后尝试以下操作:
1 | DisplayMemberPath="Description" |
编辑:您也可以添加此代码以将Code用作值。
1 | SelectedValuePath="Code" |