在具有依赖项属性的自定义属性中,DataBinding未启用

时间:2011-09-08 17:32:40

标签: c# silverlight silverlight-4.0 dependency-properties

我想创建自己的silverlight dll以添加到另一个项目。

为此reasin我创建一个Silverlight LibraryControl包含一些textBox和combobox以及一个对象列表(可观察的集合类型)

我尝试为它们创建DependencyProperty Type对象。

现在我想要在我的第二个项目中,我可以使用DataBinding填充这些属性,但是我将其添加到Project the Databinding并且其他一些禁用了。

我的代码如下所示

 public static readonly DependencyProperty DPDescription = DependencyProperty.Register("DesCription", typeof(string), typeof(WorkFlowPfazar), new PropertyMetadata(Description_Changed));
    public string Description
    {
        get
        {
            return (string)GetValue(DPDescription);
        }
        set
        {
            SetValue(DPDescription, value);
        }
    }
    private static void Description_Changed(DependencyObject Object, DependencyPropertyChangedEventArgs Args)
    {
        WorkFlowPfazar wf = Object as WorkFlowPfazar;
        if (wf == null)
            return;
        wf.tbDescription.Text = Args.NewValue.ToString();
    }


    public static readonly DependencyProperty DPFormNames = DependencyProperty.Register("FormNames", typeof(ObservableCollection<string>), typeof(WorkFlowPfazar),new PropertyMetadata(FormNames_Change));
    public ObservableCollection <object> FormNames
    {
        get
        {
            return (ObservableCollection<object>)GetValue(DPFormNames);
        }
        set
        {
            SetValue(DPFormNames, (ObservableCollection <object>)value);
        }
    }
    private static void FormNames_Change(DependencyObject Object, DependencyPropertyChangedEventArgs Args)
    {
        WorkFlowPfazar wf = Object as WorkFlowPfazar;
        if (wf == null)
            return;
        wf.cbFormName.ItemsSource = Args.NewValue as ObservableCollection <object>;
    }

还有更多这样的属性。但我发布了两个问题来解决问题。 问题是什么?或者我该怎么做?

1 个答案:

答案 0 :(得分:0)

在Silverlight中,编码约定很重要。保存属性的DependencyProperty值的字段应该与属性具有相同的名称以及后缀“Property”。传递给Register方法的名称也应该与属性的名称匹配。例如,您的Description属性应如下所示: -

    public static readonly DependencyProperty DescriptionProperty = 
       DependencyProperty.Register(
          "Description",
          typeof(string),
          typeof(WorkFlowPfazar),
          new PropertyMetadata(Description_Changed));

    public string Description
    {
        get
        {
            return (string)GetValue(DescriptionProperty);
        }
        set
        {
            SetValue(DescriptionProperty, value);
        }
    }

    private static void Description_Changed(DependencyObject Object, DependencyPropertyChangedEventArgs Args)
    {
        WorkFlowPfazar wf = Object as WorkFlowPfazar;
        if (wf == null)
            return;
        wf.tbDescription.Text = Args.NewValue.ToString();
    }
相关问题