绑定两个自定义依赖项对象

时间:2012-11-19 08:00:00

标签: wpf data-binding

我有一个在xaml中创建的自定义依赖对象,我想将它的整个实例绑定到另一个自定义依赖对象的属性。控制中发生的一切。 像这样的例子

class row : dependencyobject
{
   // this is a dp property
   public static readonly dependencyproperty widthproperty = dependencyproperty.register...
}

class cell : dependencyobject
{

   public static readonly dependencyproperty dataproperty = dependencyproperty.register....
}

class customGridView:Control
{
  List<Row>Rows { get;set;}
  List<Cell> Cells { get;set;}
}

现在我想将一个行的实例绑定到cell.data属性,也是这样的:

<customGridView>
 <row x:name:row1 width=200/> (this is a class that derives for dependencyobject)
 <row .../>
 <cell data={Binding ElementName=row1}/> (this is another class that derives from dependencyobj and tries to bind to entire instance of row class)
 <cell ..../>
 <cell ..../>
</customGridView/>

任何帮助?

根据需要,这是cell.data:

的dp属性
 public Row Data
    {
        get { return (Row)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ColumnName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty=
        DependencyProperty.Register("Data", typeof(Row), typeof(Cell), new PropertyMetadata(null));

2 个答案:

答案 0 :(得分:1)

现在我得到你的问题就像这样做

  <cell.data>
    <x:Reference Name="row1"/>
  </cell.data>

答案 1 :(得分:0)

看看如何实施GridRowDefinition

  • 您的RowCell类必须从继承自DependencyObject的{​​{3}}继承,并定义Name属性,允许您的实例在稍后会找到适当的FrameworkContentElement
  • 您需要将这些实例链接到INameScope,因为Cell中的绑定依赖于它来查找'row1'。有关详细说明,请参阅logical tree