I am trying to add a ComboBox in the code to a WPF UserControl. The ComboBox is added as expected and shows the default I set in ComboBox, but when I press it, it does not open up.
This is the code with the Data that is trying to get bound, only part of it:
public class SelectRecentData : INotifyPropertyChanged
{
public ObservableCollection<ComboBoxItem> projectItems { get; set; }
private ComboBoxItem _selectedprojectItem;
public ComboBoxItem SelectedprojectItem
{
get
{
return _selectedprojectItem;
}
set
{
this._selectedprojectItem = value;
this.onPropertyChanged("SelectedprojectItem");
}
}
}
This is the code for generating and inserting the ComboBox:
SelectRecentData Data = new SelectRecentData();
ComboBox cbo = new ComboBox();
cbo.SetBinding(ComboBox.ItemsSourceProperty,new Binding(){
bind.Source = Data; //Data is the class mentioned above
bind.Path = new PropertyPath("projectItems");}
cbo.SetBinding(ComboBox.SelectedItemProperty,new Binding(){
bind.Source = Data;
bind.Path = new PropertyPath("SelectedprojectItem");});
cbo.Name = name;
cbo.Margin = new Thickness(0.5,0.5,0.5,0.5);
cbo.Width = 200;
sp.Children.Add(cbo); //sp is the StackPanel
This is the code for test adding items if you press a button on the page:
var defaultvalue = new ComboBoxItem { Content = "Added "+mycounter };
Data.projectItems.Add(defaultvalue);
Data.SelectedprojectItem = defaultvalue;
mycounter++;