WinRT中的自定义控件和TemplateBinding

时间:2012-11-08 15:59:37

标签: c# custom-controls windows-runtime winrt-xaml

我正在尝试创建一个显示圆圈的简单自定义控件。此控件具有Radius属性,但遗憾的是它不适用于该控件。这是一个模板:

<Style TargetType="local:SizedCircle">
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:SizedCircle">
                <Border
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <Ellipse Width="{TemplateBinding Radius}" Height="{TemplateBinding Radius}">
                        <Ellipse.Fill>
                            <SolidColorBrush Color="Red"/>
                        </Ellipse.Fill>
                    </Ellipse>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

    namespace CustomControls
{
    public sealed class SizedCircle : Control
    {
        public SizedCircle()
        {
            this.DefaultStyleKey = typeof(SizedCircle);
        }

        public string Radius
        {
            get { return (string)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Radius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusProperty =
            DependencyProperty.Register("Radius", typeof(string), typeof(SizedCircle), new PropertyMetadata(null));
}
}

然后我尝试使用这个控件:

 <local:SizedCircle Radius="50" />

但我在屏幕上看不到任何内容。此Radius属性不适用。我错了什么?

1 个答案:

答案 0 :(得分:3)

尝试将属性类型更改为double而不是string。