繁体   English   中英

无法在WPF中绑定从DependenyObject派生的类的DependencyProperty?

[英]Can't Bind the DependencyProperty of a Class Derived from DependenyObject in WPF?

我有一个名为MyControl的控件,其中包含一个名为MyClasses的Collection < MyClass >

   <MyControl>
      <MyClasses>
         <MyClasss Value={Binding} />
      </MyClasses>
  </MyControl>

DependencyObject派生的MyClass

        Public Class MyClass : DependencyObject,INotifyPropertyChanged
        {
            Value  //propdp (DependencyProperty) 
        }

我有一个DataTable 我需要将表[0] [0]中存在的数据与MyClass的Value属性绑定在一起

        Binding valuebinding = new Binding();
        valuebinding.Path = new PropertyPath("ItemArray[0]");
        valuebinding.Source = Table.Rows[0];
        valuebinding.Mode = BindingMode.TwoWay;
        BindingOperations.SetBinding(myValue, MyClass.ValueProperty, valuebinding);
        BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, valuebinding);

现在我需要动态更改数据表中存在的数据的值吗? 有问题吗? 解决方案是否与以下原因有关?

  1. DependencyObject类重写并密封Equals()和GetHashCode()方法
  2. DependencyObjects未标记为可序列化
  3. DependencyObject具有线程关联性–只能在创建它的线程上访问

注意:它在Silverlight中工作正常

您最好可以做一件事。 创建一个具有要从dataTable对象设置的属性的类,从INotifyPropertyChanged派生该类,然后从控件绑定该类的属性。 一个小的代码段,如下所示

class MyCodeSnippet:INOtifyPropertyChanged
{
   public object MyProperty {
     get; 
     set
      {
        //Register for Notification
      }
    }

   //Implement all the INotifyPropertyChanged function
}


valuebinding.Path = new PropertyPath("MYProperty");
valuebinding.Source = MyClass;

暂无
暂无

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

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