【问题标题】:Why can't I binding a array to a dependencyproperty using c#?为什么我不能使用 c# 将数组绑定到依赖属性?
【发布时间】:2012-07-01 14:55:35
【问题描述】:

我的工作
创建一个用户控件,它的 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'

如何
为什么会出现这个错误,我没有将它的源类型注册到字符串数组吗?如何解决?

【问题讨论】:

  • 您的资源类型必须是string[],而不是string

标签: c# silverlight windows-phone-7 data-binding dependency-properties


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    至于错误,@Michael 和@herzmeister 是对的,你忘了[]。但为避免出现性能警告CA1819: Properties should not return arrays,如果可以的话,我建议您更改属性以返回集合。

    【讨论】:

      猜你喜欢
      • 2012-01-07
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多