Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateBinding doesn't bind to effect's property?

Imagine a control named Testko like this:

public class Testko: Control
    {
        public static readonly DependencyProperty TestValueProperty;

        static Testko()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Testko), new FrameworkPropertyMetadata(typeof(Testko)));
            TestValueProperty = DependencyProperty.Register("TestValue", typeof(double), typeof(Testko), new UIPropertyMetadata((double)1));
        }

        public double TestValue
        {
            get { return (double)GetValue(TestValueProperty); }
            set { SetValue(TestValueProperty, value); }
        }
    }

Nothing fancy, just an empty control with a single double property with a default value set to (double)1. Now, image a generic style like this:

<Style TargetType="{x:Type local:Testko}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Testko}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel Orientation="Vertical">
                        <StackPanel.Effect>
                            <BlurEffect Radius="{TemplateBinding TestValue}" />
                        </StackPanel.Effect>
                        <Button Content="{TemplateBinding TestValue}" Margin="4" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now, the problem is that Radius property is never bound for some reason. Wheras Button's content is properly bound to TestValue property. I am sure I am missing something obvious. Or not?

like image 857
Miha Markic Avatar asked Mar 06 '10 12:03

Miha Markic


1 Answers

If it is obvious, it is not to me :-)

My favorite book (WPF Unleashed) mentions that sometimes TemplatedBinding doesn't work (but the enumerated reasons don't match your circumstances).

But TemplatedBinding is a shortcut for:

{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TestValue}

I reproduced your case, i.e. changing TestValue only has effect on the button. After replacing the TemplatedBinding by this, I get the desired effect.

like image 193
Timores Avatar answered Sep 21 '22 01:09

Timores