【发布时间】:2012-03-27 00:17:37
【问题描述】:
我遇到了一个问题,我创建了一个包含两个内容集合的用户控件。为简单起见,我们将其称为两个项目控件。
在后面的代码中,我公开了这些 itemCollections,以便我可以在另一个控件中实际声明内容。
例如
<!-- User Control xaml -->
<UserControl>
<StackPanel Orientation="Horizontal" >
<ItemsControl x:Name="_itemsControl1" />
<ItemsControl x:Name="_itemsControl2" />
</StackPanel>
</UserControl>
//in the codebehind for user control
public partial class TwoControls
{
public ItemCollection ItemsOne { get { return _itemsControl1.Items; }}
public ItemCollection ItemsTwo { get { return _itemsControl2.Items; }}
}
<!-- Using the control in xaml later -->
<Custom:TwoControls>
<Custom:TwoControls.ItemsOne>
<TextBox />
<TextBox />
<TextBox />
<TextBox />
<TextBox />
</Custom:TwoControls.ItemsOne>
<Custom:TwoControls.ItemsTwo>
<Button />
<Button />
<Button />
<Button />
<Button />
</Custom:TwoControls.ItemsTwo>
<Custom:TwoControls>
这实际上对一个小问题很有效。一旦我尝试命名任何控件,我就会收到以下错误。
<!-- Using the control in xaml later -->
<Custom:TwoControls>
<Custom:TwoControls.ItemsOne>
<TextBox x:Name="txt"/>
无法在元素“TextBox”上设置名称属性值“txt”。 “TextBox”在元素“TwoControls”的范围内,当它在另一个范围内定义时,它已经注册了一个名称。
如果我实际上不必为控件命名,我就不会。我们有一些工具在运行时期望某些内容控件被命名,因此作为构建过程的一部分,我需要它们具有名称。还值得注意的是,我实际上在我的 TwoControls 类中绑定了几个事件,如果我要将其提取到数据模板中,我认为我可以使它工作,但我必须比现在多做一点工作。
任何关于为什么会这样的意见都会很棒。
【问题讨论】:
标签: wpf silverlight xaml