views:

142

answers:

1

I have a user control - say "ControlBase". It has "SomeItems" property, which is an ObservableCollection<InheritedFromDO>, where InheritedFromDO is a class inherited from "DependencyObject".
When I create markup for a child class of the ControlBase i'd like to initiate the "SomeItems" collection. But somehow I cannot use bindings in that markup, although the control has a pretty normal DataContext and binding works in normal cases.

It looks like this:

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

The output says:

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

Any ideas how to make it work?

+1  A: 

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

HTH, Kent

Kent Boogaart
Thanks Kent, that's exactly what i needed. It also seems to resolve some issues i had with Resources resolution
arconaut
although, it still works properly only if objects are inherited from FrameworkElement, not just DependencyObject
arconaut