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

しかし、画面には何も表示されません。この Radius プロパティは適用されません。私は何を間違っていますか?

4

1 に答える 1

3

プロパティタイプを文字列ではなくdoubleに変更してみてください。

于 2012-11-08T18:24:52.870 に答える