【问题标题】:DataBinding is not Enable in my Custom Property with Dependency Property在具有依赖属性的自定义属性中未启用 DataBinding
【发布时间】:2011-11-13 05:03:02
【问题描述】:

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

为此,我创建了一个 Silverlight LibraryControl,其中包含一些文本框和组合框以及一个对象列表(可观察的集合类型)

我尝试为它们创建 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>;
    }

还有一些类似的属性。但我将两个一发布到一个简单的问题。 问题是什么?或者我该怎么办?

【问题讨论】:

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


【解决方案1】:

在 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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-04
    • 2011-09-09
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2012-08-29
    相关资源
    最近更新 更多