I have a 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>
It works, but the ugliness of Slot Binding bothers me. This is necessary because GroupPrototypeSlotControl has a GroupPrototypeViewModel as its DataContext. If I just use it {Binding SelectedSlot}, it tries to resolve it on the "child" ViewModel, which fails. I wrap this around explicitly looking at the DataContext of my parent control. Is there a cleaner way to do this type of binding?
EDIT: I found a cleaner way to solve my problem, although it still looks like a hack. I modified GroupPrototypeSlotControl so that in this case there is a top-level LayoutRoot (in this case, StackPanel), and then set the DataContext LayoutRoot to ViewModel, and not set the DataContext for the entire control. This allows me to use the syntax {Binding SelectedSlot}where I use the control (since the control still has a parent DataContext), due to a slight increase in the complexity of the control. In general, this is probably the best template for a custom control, since the consumer of the control expects {Binding} to be resolved to their parent DataContext if it is not explicitly specified.