c# - Silverlight 4 - 通过 XAML 声明/填充控件的集合属性?

标签 c# silverlight silverlight-4.0 collections user-controls

首先,我要感谢大家提供的所有精彩信息。我不得不承认 - StackOverflow 一直是可用的最好的同伴导师资源,因此它为我提供了比......“大学”4 年更多的知识。谢谢!

我正在使用一个控件,该控件具有一个对象集合属性。

public class UserParameter
{
    string DisplayName { get; set; }
    string Property { get; set; }
    string Type { get; set; }
}

public class ParameterBuilder: UserControl
{
    private ObservableCollection<UserParameter> parameters;

    //alright - this is really dependency property.
    //described as property just for simplicity.
    public ObservableCollection<UserParamter> Parameters
    {
        get { return this.parameters; }
        set { this.parameters = value; }
    }
}

所以这个问题的核心是弄清楚如何在 Xaml 中创建这个集合。例如:

<custom:ParameterBuilder Name="Parameter">
    <custom:ParameterBuilder.Parameters>
        <custom:UserParameter DisplayName="Test 0" Property="Size"  Type="String"/>
        <custom:UserParameter DisplayName="Test 1" Property="Value" Type="Decimal"/>
    </custom:ParameterBuilder.Parameters>
</custom:ParameterBuilder>

这可能吗?如果可能,我该怎么做?

最佳答案

如果您使用的是 .NET 4.0,您应该能够使用 x:TypeArguments 参数 (part of the XAML2009 spec) 引用泛型 - 因此您的参数中的 Observable 集合将声明为:

<ObservableCollection x:TypeArguments="UserParameter">
    <l:UserParameter DisplayName="Test 0" Property="Size" Type="String" />
    <l:UserParameter DisplayName="Test 1" Property="Value" Type="Decimal" />
</ObservableCollection />

关于c# - Silverlight 4 - 通过 XAML 声明/填充控件的集合属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6297444/

相关文章:

c# - WPF 中的 ValidationRule 与行为

c# - 如何隐藏 ListView 控件中的列?

c# - SQL : Update a row and returning a column value with 1 query

c# - 使用 MVVM 模式的 UI 设计

silverlight - 使用 WebClient() 从 Silverlight 访问 RSS 提要时出错

c# - 在 c# 中为 Vector256 准备数据的最快方法是什么?

VS2008 中的 Silverlight MVVM - 非入门者?

银光 4 : Reliable Commanding with RequerySuggested Functionality?

mvvm - 带有 Prism 4 的新 LOB Silverlight 4(mvvm、mef、unity)

c# - 在 Silverlight 中实现 IValueConverter 接口(interface)