我正在为我的一个用户控件制作规则列表。该列表包含自定义类型List<StringInputRule>
。我正在使用DependancyProperty
进行数据绑定。
我尝试在XAML中设置控件的规则如下:
<controlsDefault:DateEditWithStringInput>
<controlsDefault:DateEditWithStringInput.Rules>
<x:Array Type="controlsDefault:StringInputRule" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<controlsDefault:StringInputRule Key="T" Amount="1" Unit="Day"/>
<controlsDefault:StringInputRule Key="W" Amount="1" Unit="Week"/>
<controlsDefault:StringInputRule Key="M" Amount="1" Unit="Month"/>
</x:Array>
</controlsDefault:DateEditWithStringInput.Rules>
</controlsDefault:DateEditWithStringInput>
c#代码,用于控件的依赖属性:
public partial class DateEditWithStringInput : UserControl
{
public DateEditWithStringInput()
{
InitializeComponent();
Rules = new List<StringInputRule>();
}
public IList<StringInputRule> Rules
{
get { return (IList<StringInputRule>)GetValue(RulesProperty); }
set { SetValue(RulesProperty, value); }
}
public static readonly DependencyProperty RulesProperty =
DependencyProperty.Register("Rules", typeof(IList<StringInputRule>), typeof(DateEditWithStringInput), new PropertyMetadata(new List<StringInputRule>()));
}
以下方法不传递任何值,但它是可编译的。我已经阅读到从2009年4.0版本开始,泛型类型可以在xaml中初始化,但我没有找到一个例子。
我的问题是:如何在xaml中定义泛型List?
编辑:截至目前,该问题还没有确定的解决方案。
解决方法(由Szabolcs Dézsi指出):how use List<T> within xaml?