【发布时间】:2016-02-03 17:10:25
【问题描述】:
我正在尝试为我的一个用户控件创建一个规则列表。 List 包含一个自定义类型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?
编辑:截至目前,该问题还没有可靠的解决方案。
WorkAround(由 Szabolcs Dézsi 指出):how use List<T> within xaml?
【问题讨论】:
标签: c# wpf xaml generics .net-4.5