在 xaml 4.5+ 中创建通用列表

Create a generic list in xaml 4.5+

我正在尝试为我的一个用户控件创建一个规则列表。列表包含自定义类型 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>()));
}

所以下面的方法不传递任何值,但它编译。我读到泛型类型可以在 xaml 中从 2009 版 4.0 开始初始化,但我找不到示例。

我的问题: 如何在 xaml 中定义泛型列表?

编辑:问题到现在还没有解决方案。

解决方法(如 Szabolcs Dézsi 所指出):how use List<T> within xaml?

看起来像这样:

<controlsDefault:DateEditWithStringInput.Rules>
    <generic:List x:TypeArguments="controlsDefault:StringInputRule">
        <controlsDefault:StringInputRule Key="T" Amount="1" Unit="Day"/>
        <controlsDefault:StringInputRule Key="W" Amount="1" Unit="Week"/>
        <controlsDefault:StringInputRule Key="M" Amount="1" Unit="Month"/>
    </generic:List>
</controlsDefault:DateEditWithStringInput.Rules>

x:TypeArguments is a XAML 2009 feature. Unfortunately it's not supported in compiled XAML, so you won't be able to use it in your WPF application. Read more here, here and here.