2009-04-28 82 views
0

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

它看起来像这样:

<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')

任何想法如何使它工作吗?

回答

1

这是因为集合中的项目不是逻辑树的一部分。如果你想要这样做,你需要控制你的customize the logical children,或者为你做一个控制(如ItemsControl)。

+0

谢谢肯特,这正是我所需要的。它似乎也解决了我在资源分辨率为 – arconaut 2009-04-28 12:45:14