1

我正在尝试创建一个显示圆圈的简单自定义控件。此控件具有 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" />

但我在屏幕上什么也看不到。不应用此半径属性。我在做什么错?

4

1 回答 1

3

尝试将属性类型更改为双精度而不是字符串。

于 2012-11-08T18:24:52.870 回答