开发者

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜