2009-04-28 81 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