且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF组合框绑定从代码后面

更新时间:2023-10-19 19:28:28

>

  myComboBox.SetBinding(
ItemsControl.ItemsSourceProperty,
new Binding {Source = myList});

myComboBox.SetBinding(
Selector.SelectedItemProperty,
new Binding(SelectedItem){Source = mySelectedItemSource});


How can I bind an IList item to a ComboBox through the codebehind.cs file. Please explain with simple steps.

I need the steps for two way binding, not by setting ItemsSource.

I need something like:

myComboBox.SetBinding(ItemsControl.ItemsSourceProperty, 
                      new Binding { Source = myList });

but I need the SelectedItem also.

Thanks

Do you mean something like this?

myComboBox.SetBinding(
   ItemsControl.ItemsSourceProperty, 
   new Binding { Source = myList });

myComboBox.SetBinding(
   Selector.SelectedItemProperty,
   new Binding("SelectedItem") { Source = mySelectedItemSource});