views:

84

answers:

1

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?

+2  A: 

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.

Timores
After answering, I found http://stackoverflow.com/questions/1131222/wpf-templatebinding-vs-relativesource-templatedparent whose answer is quite interesting regarding the difference between Binding and TemplateBinding
Timores
Seems like a some kind of bug to me. Thanks for the solution :-)
Miha Markic
Yay! You're my hero! :) I was having a similiar problem and that fixed it. :)
townsean