gpt4 book ai didi

c# - 在 xaml 4.5+ 中创建通用列表

转载 作者:太空宇宙 更新时间:2023-11-03 15:26:19 24 4
gpt4 key购买 nike

我正在尝试为我的一个用户控件创建一个规则列表。列表包含自定义类型 List<StringInputRule> .我正在使用 DependancyProperty进行数据绑定(bind)。

我正在尝试在 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 中定义泛型列表?

编辑:直到今天,这个问题还没有可靠的解决方案。

解决方法(如 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是 XAML 2009 功能。不幸的是,编译后的 XAML 不支持它,因此您将无法在 WPF 应用程序中使用它。阅读更多 here , herehere .

关于c# - 在 xaml 4.5+ 中创建通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35183412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com