小编Mih*_*kic的帖子

TemplateBinding不绑定到effect的属性?

想象一下名为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); }
        }
    }
Run Code Online (Sandbox Code Playgroud)

没什么好看的,只是一个带有单个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>
Run Code Online (Sandbox Code Playgroud)

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

wpf binding templates effects

6
推荐指数
1
解决办法
1147
查看次数

标签 统计

binding ×1

effects ×1

templates ×1

wpf ×1