繁体   English   中英

为什么我不能使用C#将数组绑定到依赖项属性?

[英]Why can't I binding a array to a dependencyproperty using c#?

我所做的
创建一个用户控件,其DependencyProperty之一绑定到字符串数组

    public static readonly DependencyProperty TaskListProperty =
    DependencyProperty.Register("TaskList",
                                typeof(string[]),
                                typeof(MainControl),
                                null);

    public string TaskList
    {
        get { return (string[])GetValue(TaskListProperty); }
        set { SetValue(TaskListProperty, value); }
    }

我得到了什么
此代码错误

    get { return (string[])GetValue(TaskListProperty); }

错误:无法将类型'string []'隐式转换为'string'

怎么样
为什么会发生此错误,我不将其源类型注册到字符串数组吗? 怎么解决呢?

错误消息是正确的。 您忘记了属性类型的[]

public string[] TaskList
{
    get { return (string[])GetValue(TaskListProperty); }
    set { SetValue(TaskListProperty, value); }
}

至于错误,@Michael和@herzmeister是正确的,您忘记了[] 但是为了避免性能警告CA1819:属性不应返回数组如果可以的话,我建议您将属性更改为返回集合。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM