I have a custom control, Cake, that contains two DependencyProperties named Slice and Filling. How do I create a style that gives me access to Slice, but also lets me design slice?
<Style TargetType={x:Type local:Cake}>
//I don't like setting DataContext Here
<Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType={x:Type local:Cake}>
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
//This is how I display a slice
<ContentPresenter Content={Binding Slice}/>
//This is how cake decorations are displayed
<ItemsPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Slice">
<Setter.Value>
//Design Slice Here - it's easy to override when I want
<Slice Filling={Binding Filling}> // it's just in a setter.
</Setter.Value>
</Setter>
<Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}/>
</Style>
Options I've tried:
I can't use a UserControl, because I want to allow named content, which apparently doesn't work with User Controls. See Here.
I don't like the example above because I have to set the DataContext of the Cake container to self, meaning a user can't use the DataContext for their bindings.
I can't bind the Filling property using RelativeSource, because with several cakes, the Style wouldn't know which one was the correct parent. See Here.
I could replace the Content Presenter directly with a Slice Element, but because it is in a template, I loose access to the Slice Anywhere outside the Template. While I could probably typecast my way down the visualTree to the slice, this feels a maintenance nightmare.
I basically want each cake to have a slice, and to be able to set it using
<Cake.Slice>
<DockPanel>
<Rectangle Background= “Blue”/>
<Rectangle Background= “Blue”/>
<Rectangle Background=“{Binding Filling}”/>
</DockPanel>
</Cake.Slice>
while also giving it a default look.
EDIT: Apparently my style DOES work, provided that I reference the Cake.dll as opposed to the Cake project. Why would that be?