User avatar
stonstad
Topic Author
Posts: 183
Joined: 06 Jun 2016, 18:14
Location: Lesser Magellanic Cloud
Contact:

Effect Template Binding

05 Jan 2022, 21:32

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" />
 
User avatar
sfernandez
Site Admin
Posts: 2569
Joined: 22 Dec 2011, 19:20

Re: Effect Template Binding

07 Jan 2022, 12:52

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}}"/>
 
User avatar
stonstad
Topic Author
Posts: 183
Joined: 06 Jun 2016, 18:14
Location: Lesser Magellanic Cloud
Contact:

Re: Effect Template Binding

11 Jan 2022, 19:57

Thank you!

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 1 guest