TemplateBinding不绑定到effect的属性?

时间:2010-03-06 12:21:40

标签: wpf templates binding effects

想象一下名为Testko的控件就像这样:

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); }
        }
    }

没什么好看的,只是一个空控件,只有一个double属性,默认值设置为(double)1。 现在,像这样形成一个通用的样式:

<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>

现在,问题是Radius属性由于某种原因从未绑定。 Wheras Button的内容已正确绑定到TestValue属性。 我相信我错过了一些明显的东西。或者不是?

1 个答案:

答案 0 :(得分:9)

如果很明显,那不是我: - )

我最喜欢的书(WPF Unleashed)提到有时TemplatedBinding不起作用(但列举的原因与你的情况不符)。

但TemplatedBinding是以下的快捷方式:

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

我重现了你的情况,即改变TestValue只对按钮产生影响。 通过这个替换TemplatedBinding后,我得到了预期的效果。