I'm trying to get to grips with ControlTemplates in WPF. I noticed that when you try to reference a templated parent's background color - the binding statement seems to be different for elipse Fill and elipse GradientStop. Can any one explain why this is.
This works...
<Ellipse RenderTransformOrigin=".5,.5" Fill="{TemplateBinding Background}">
This works...
<GradientStop Offset="0" Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background.Color}"/>
This doesnt work - why is this?
<GradientStop Offset="0" Color="{TemplatedParent Background}"/>
This produces an error...
<GradientStop Offset="0" Color="{TemplatedParent Background.Color}"/>
This doesnt work...
<Ellipse RenderTransformOrigin=".5,.5" Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background.Color}">
The instance looks like this...
<Button Template="{StaticResource buttonTemplate1}"
Height="100" Width="100" FontSize="40"
Background="Violet" Foreground="Aquamarine"
Padding="0" Margin="6">Button 5</Button>
As mentioned in comments, you need to distinguish between
BrushandColortype properties.Brush-typed properties are used by controls, and includeFill,Stroke, andBackground.Coloris as a component of aBrush, either the color of aSolidColorBrush, or the color of aGradientStopof aLinearGradientBrush.There are also some binding syntax errors in the examples you posted.
That's not a valid binding syntax. But assuming you meant
TemplateBinding Background, then it's still not valid, because the targetColoris a different type than the sourceBackground(Brush).Same issue as above, you mean
TemplateBindinginstead ofTemplatedParent. And, I believe that in any caseTemplateBindingwon't work for a nested property. You would need to write:The syntax is correct here, but the types don't match -- the target is
Brush, and the source isSystem.Color.