0

我有一个用户控件 - 说“ControlBase”。它具有“SomeItems”属性,这是一个ObservableCollection<InheritedFromDO>InheritedFromDO“DependencyObject”继承的类。
当我为子类创建标记时,ControlBase我想启动“SomeItems”集合。但不知何故,我无法在该标记中使用绑定,尽管控件具有非常正常的 DataContext 并且绑定在正常情况下有效。

它看起来像这样:

<local:ControlBase
   ...
   >
   <local:ControlBase.SomeItems>
     <SomeItem
       DepPropertyOne={Binding Id} <!-- Does NOT work here -->
       />
     <SomeItem
       DepPropertyOne={Binding Name} <!-- Does NOT work here -->
       />
   <local:ControlBase.SomeItems>

   <Grid>
     <TextBlock
       Text={Binding Id} <!-- Works here -->
       />
   </Grid>
</local:ControlBase>

输出说:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Id; DataItem=null; target element is 'SomeItem' (HashCode=26965808); target property is 'DepPropertyOne' (type 'Object')

任何想法如何使它工作?

4

1 回答 1

1

That's because the items in the collection are not part of the logical tree. You need to customize the logical children of your control if you want that to work, or subclass a control that does it for you (eg. ItemsControl).

于 2009-04-28T10:34:00.783 回答