我正在尝试构建一个能够从XAML中获取元素的usercontrol:
<ComboBox >
<ComboBoxItem />
<ComboBoxItem />
<ComboBoxItem />
</ComboBox>
在ComboBox
中,您只需在ComboBox
代码之间添加项目,我想复制一下,但我不知道从哪里开始。
完成它应该是这样的:
<cis:ReportControl Grid.Row="3">
<cis:ReportItem />
</cis:ReportControl>
在cis:ReportControl
中,有一些Button
和一个ComboBox
,基本上我只想向ComboBox
提供项目。
报告项目只是ComboBoxItem
,有一些额外的属性。
修改
我已根据@Snowbears的答案实现了它,但现在的问题是控件本身就是一个项目。
我认为这是因为我有一些内容,通过将ContentProperty
定义为我的ComboBox
,它会被重定向到Box中。
我该怎么做才能避免这种情况?
编辑II:
现在完全有效:
private ItemCollection reportItems;
public ItemCollection ReportItems
{
get
{
if (reportItems == null)
{
reportItems = this.ComboBoxReports.Items;
}
return reportItems;
}
}
使用[ContentProperty("ReportItems")]
属性。 ComboBoxReports是Control中的ComboBox,我必须继承ItemsControl
才能使它工作。
答案 0 :(得分:4)
IList
接口的内容。假设此属性将命名为ReportItems
。此属性不应该有setter,它应该在UserControl本身中初始化,或者在构造函数中通过字段初始化在后备字段上。 ReportItems
)ItemsSource
绑定到UserControl的ReportItems
属性答案 1 :(得分:0)
如果你寻找How Create own Control
你必须寻找两件事:
1)Custom Control 1或User Control 1(取决于您的need)
2)Dependency Properties(在控制中使用它们)
我认为您可能需要使用自定义控件,并且您还可以从ComboBox
或其他控件继承自定义控件。