2012-11-08 130 views
1

我試圖創建一個簡單的自定義控件,顯示一個圓圈。此控件具有Radius屬性,但不幸的是它不適用於該控件。這裏是一個模板:WinRT中的自定義控件和TemplateBinding

<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屬性未應用。我什麼錯了?

回答

3

嘗試將屬性類型更改爲double而不是字符串。