Is there a cleaner way to Bind property to owner's DataContext?

Posted by Dan Bryant on Stack Overflow See other posts from Stack Overflow or by Dan Bryant
Published on 2010-04-27T17:54:08Z Indexed on 2010/04/27 19:53 UTC
Read the original article Hit count: 291

Filed under:
|
|
|
|

I have some code that looks like this:

<Expander Header="{Binding SelectedSlot.Name}"
          Visibility="{Binding ShowGroupSlot, Converter={StaticResource BooleanToVisibility}}">
    <Controls:GroupPrototypeSlotControl Slot="{Binding DataContext.SelectedSlot, 
        RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type Expander}}}" />
</Expander>

This works, but the ugliness of the Slot Binding bothers me. This is required because the GroupPrototypeSlotControl has a GroupPrototypeViewModel as its DataContext. If I simply use {Binding SelectedSlot}, it attempts to resolve it on the 'child' ViewModel, which fails. I get around this by explicitly looking at the DataContext of my parent control. Is there a cleaner way to do this type of binding?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding