开发者

WPF: how to bind ComboBox ItemsSource in code?

I need to convert this following XAML into code-behind:

<ComboBox SelectedItem="{Binding Level}" Items开发者_JAVA技巧Source="{Binding Levels}" />

However, this code doesn't compile:

new ComboBox() { SelectedItem = new Binding("Level"), ItemsSource = new Binding("Levels") }

The error: "Cannot implicitly convert type 'System.Windows.Data.Binding' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)". How do I cast?


ComboBox cbo=new ComboBox();
cbo.SetBinding(ComboBox.SelectedItemProperty,new Binding("Level"){ /* set properties here*/});
cbo.SetBinding(ComboBox.ItemsSourceProperty,new Binding("Levels"));
....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜