【发布时间】:2010-04-06 14:24:33
【问题描述】:
在我的 Silverlight 应用程序中,我有一个事件处理程序,它动态创建用户控件的新实例并将其添加到另一个控件的 Content 属性中。这工作得很好,没有错误,直到我为用户控件分配了一个名称 (x:Name="")(不是在事件中,而是在用户控件的 XAML 中。然后,当我将模板设置为 .Content 属性时我得到一个“System.ArgumentException 未被用户代码处理/值不在预期范围内。”异常。我不知道错误在哪里或在哪里看......它几乎嵌入在 SL 中。不知道我需要设置什么其他属性。
这是添加用户控件的代码:
TilePane p = e.Element as TilePane; // this is the target
AppTileNormalViewControl template = new AppTileNormalViewControl(); // this is the user control
p.Content = template; // error happens here
这里的问题是用户控件有一些公共属性,我将使用这些属性将数据绑定到用户控件中的某些控件。我需要一个名称来执行语法:
Text="{Binding ElementName=UserControlName, Path=PublicProperty}"
如果没有 ElementName,绑定将无法正常工作。因此,如果我指定名称绑定将起作用,但我无法添加控件。想法?
【问题讨论】:
标签: silverlight