Page 1 of 1

Effect Template Binding

Posted: 05 Jan 2022, 21:32
by stonstad
Should I be able to template bind color to an effect object? I am able to bind other properties but with effects and color I can't seem to get it to work. I also tried brushes using SolidColorBrush. Thank you!

// Style
<Style TargetType="local:RadialRangeControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:RadialRangeControl">
                                <Path Name="PART_Value"
                                    <Path.Effect>
                                        <DropShadowEffect BlurRadius="15" ShadowDepth="0" Opacity="1" Color="{TemplateBinding ValueShadowColor}"/>
                                    </Path.Effect>
                                </Path>
                            </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
// User Control
 
	public static readonly DependencyProperty ValueShadowColorProperty =
            DependencyProperty.Register(nameof(ValueShadowColor), typeof(Color), typeof(RadialRangeControl), new PropertyMetadata(Colors.Transparent));
           
        public Color ValueShadowColor
        {
            get { return (Color)GetValue(ValueShadowColorProperty); }
            set { SetValue(ValueShadowColorProperty, value); }
        }
// usage
 <local:RadialRangeControl ValueShadowColor="Red" />

Re: Effect Template Binding

Posted: 07 Jan 2022, 12:52
by sfernandez
TemplateBinding only works on Controls as they expose the TemplatedParent property.
If you want to bind an effect (or brush, etc.) property to the templated parent you should use a regular binding with RelativeSource:
<DropShadowEffect BlurRadius="15" ShadowDepth="0" Opacity="1"
  Color="{Binding ValueShadowColor, RelativeSource={RelativeSource TemplatedParent}}"/>

Re: Effect Template Binding

Posted: 11 Jan 2022, 19:57
by stonstad
Thank you!