【发布时间】:2014-01-03 21:48:20
【问题描述】:
我有一个带有 ComboBox 的 UserControl,并且我将 ObservableCollection 绑定到它,如下所示。现在该集合填充在 UserControl 中。但是,我想在 MainWindow 中创建 ObservableCollection,并为我的 UserControl 创建另一个构造函数。这是我现在得到的,它正在工作:
public ObservableCollection<ComboBoxInfo> Items { get; private set; }
public CustomComboBox()
{
InitializeComponent();
Items = new ObservableCollection<ComboBoxInfo>();
cmb.ItemsSource = Items;
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
this.createNameComboBox(); // ObservatoryCollection populating
}
}
我尝试实现第二个构造函数并在主窗口中移动集合填充函数,但我收到一条错误消息,提示我在 UserControl 中的组合框未设置为对象的实例。理想情况下,我想要这样的东西:
public CustomComboBox(ObservableCollection<ComboBoxInfo> Items)
{
this.Items = Items
// Not sure if the binding should be done here or in default constructor
}
知道如何正确执行此操作吗?谢谢
【问题讨论】:
标签: wpf binding constructor user-controls