在XAML中设置IEnumerable DependencyProperty会引发错误 [英] IEnumerable DependencyProperty throws errors when set in XAML

查看:97
本文介绍了在XAML中设置IEnumerable DependencyProperty会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件 Workspace ,该控件继承自 Control ,在其中是一个我需要包含用户指定的 IEnumerable< IFoo> 的DependencyProperty (我也尝试将其设为非通用的 IEnumerable )。

I have a custom control Workspace that inherits from Control and within it is a DependencyProperty that I need to contain a user-specified IEnumerable<IFoo> (I have also tried making it an non-generic IEnumerable).

Public Shared ReadOnly FoosProperty As DependencyProperty = DependencyProperty.Register("Foos", GetType(IEnumerable(Of IFoo)), GetType(Workspace), New FrameworkPropertyMetadata())
Public Property Foos() As IEnumerable(Of IFoo)
    Get
        Return CType(Me.GetValue(FoosProperty), IEnumerable(Of IFoo))
    End Get
    Set(ByVal value As IEnumerable(Of IFoo))
        Me.SetValue(FoosProperty, CType(value, IEnumerable(Of IFoo)))
    End Set
End Property

当我创建并设置 IFoo 在代码中,但是当我尝试在XAML中添加它们时出现错误。如果我添加一个 IFoo 我会收到错误消息

Everything works perfectly when I create and set an array of IFoo in code but when I try to add them in XAML I get errors. If I add a single IFoo I get the error


  1. 'FooItem'是不是属性 Foos的有效值。

在运行时。如果我尝试添加多个 IFoo 项,则在编译时会出现三个错误

at run time. If I try to add multiple IFoo items I get three errors at compile time


  1. 对象工作区已经有一个子对象,无法添加 FooItem。 工作区只能接受一个孩子。

  2. 属性 Foos不支持 FooItem类型的值。

  3. Foos属性设置了一次以上。

我读到这些错误的意思是WPF并未将xaml转换为类似的项目数组通常会的。这就是我尝试在XAML中添加项目的方式

I read the errors to mean that WPF isn't converting the xaml to an array of items like it normally would. Here is how I'm trying to add the items in XAML

<Workspace>
    <Workspace.Foos>
        <FooItem />
        <FooItem />
    </Workspace.Foos>
</Workspace>

我过去创建过类似的DependencyProperties,但从未遇到过问题,所以我猜我在缺少简单的东西。

I have created similar DependencyProperties in the past and never had a problem so I'm guessing I'm missing something simple.

感谢您的帮助!

推荐答案

到能够添加多个元素,则collection属性必须是 IList IDictionary 。当它为 IEnumerable 时,XAML解析器尝试为属性本身分配第一个值(而不是像列表那样调用 Add )并对连续项感到困惑。这是您的错误的根源。

To be able to add multiple elements the collection property has to be IList or IDictionary. When it is IEnumerable XAML parser tries to assign first value to the property itself (instead of calling Add like with lists) and gets confused about consecutive items. This is the source of your errors.

此外,如果您想从XAML填充集合,请确保您的列表已实例化,并且开头不为null,因为XAML不会为您实例化列表,只需调用添加就可以了。因此,要避免 NullReferenceException ,请摆脱 IList 属性上的setter,并从构造函数中实例化列表。

Also, if you want to populate collection from XAML, make sure that your list is instantiated and not null to begin with, since XAML will not instantiate a list for you, it'll just call Add on it. So to avoid NullReferenceException, get rid of setter on your IList property and instantiate list from constructor.

它不必是依赖项属性:

private readonly ObservableCollection<FooItem> _foos = new ObservableCollection<FooItem>();

public ObservableCollection<FooItem> Foos
{
    get { return _foos; }
}

这篇关于在XAML中设置IEnumerable DependencyProperty会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆