WPF中ComboBox绑定问题
<ComboBox Name="source_language" ItemsSource="{Binding LanguageCollection, Mode=OneWay}" SelectedValuePath="langID" DisplayMemberPath="showInfo" SelectedItem="{Binding SourceLang, Mode=TwoWay}" />
private LanguageType sourceLang; public LanguageType SourceLang { get { return sourceLang; } set { sourceLang = value; OnPropertyChanged("SourceLang"); } } private ObservableCollection<LanguageType> languageCollection; public ObservableCollection<LanguageType> LanguageCollection { get { return languageCollection; } set { languageCollection = value; OnPropertyChanged("LanguageCollection"); } }
sourceLang = new LanguageType(); sourceLang.langID = 1; sourceLang.showInfo = "中文"; languageCollection = new ObservableCollection<LanguageType>(){ new LanguageType(){langID = 1, showInfo = "中文"}, new LanguageType(){langID = 2, showInfo = "英语"}, };
<ComboBox Name="source_language" ItemsSource="{Binding LanguageCollection, Mode=OneWay}" [del]SelectedValuePath="langID" [/del]DisplayMemberPath="showInfo" SelectedItem="{Binding SourceLang, Mode=TwoWay}" />
[解决办法]
你的绑定方式有问题。参考
http://blog.csdn.net/tcjiaan/article/details/6941601
[解决办法]
string dd=(source_language.SelectedItem as LanguageType).showInfo;
[解决办法]
开始绑定的时候是空的,因为绑定数据是异步.
if(source_language.SelectedItem !=null)
{
}
[解决办法]