我的自定义属性中未启用数据绑定和依赖属性

发布于 2024-12-03 18:36:10 字数 1790 浏览 1 评论 0原文

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

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

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

现在我希望在我的第二个项目中我能够使用数据绑定填充这些属性,但是当我将其添加到项目中时,数据绑定和其他一些属性被禁用。

我的代码如下所示,

 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>;
    }

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

I wants to create my own silverlight dll to add to another project.

for this reasin I create a Silverlight LibraryControl Contains some textBox and combobox and also a list of objects ( observable collection type)

I try to Create DependencyProperty Type objects for them.

Now I wants in My second Projects I could be able to fill these Properties with DataBinding but wen I add this to Project the Databinding and some Others was disable to use.

my codes is like below

 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>;
    }

there is some more Properties like these. but I post Two Ones to simle question.
what is the problem? or what shal I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2024-12-10 18:36:10

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

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

In Silverlight coding conventions matter. A field that holds the DependencyProperty value for a property should simply have the same name as the property along with the suffix "Property". Also the name passed to the Register method should also match the name of the property. For example your Description property should look like this:-

    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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文