2013-02-19 27 views
0

我正在開發一個Silverlight CustomControl,它定義了一個名爲SpinnerSize的依賴項屬性。現在我想用TemplateBinding設置Border內的默認模板到SpinnerSize -property的寬度和高度:如何使用TemplateBinding在Silverlight中設置CustomControl的大小?

<Style TargetType="local:MyCustomControl"> 
    <Setter Property="SpinnerSize" Value="12" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:MyCustomControl"> 
       <Border 
        Width="{TemplateBinding SpinnerSize}" 
        Height="{TemplateBinding SpinnerSize}" 
        Background="Red" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

在上面例子中的SpinnerSize參考定義如下:

public static readonly DependencyProperty SpinnerSizeProperty = 
    DependencyProperty.Register(
     "SpinnerSize", 
     typeof(int), 
     typeof(MyCustomControl), 
     new PropertyMetadata(default(int))); 

public int SpinnerSize 
{ 
    get { return (int)this.GetValue(SpinnerSizeProperty); } 
    set { this.SetValue(SpinnerSizeProperty, value); } 
} 

結果是我根本沒有看到邊框。如果我手動設置邊框的寬度和高度爲一個值,一切正常。

TemplateBinding一個有效的方法來實現,或者我必須手動設置在控制OnApplyTemplate()-方法中的寬度和高度?

回答

1

您的XAML看起來很好,並且像這樣使用TemplateBinding是有效的,所以問題必須出現在您的DependencyProperty中。

高度和寬度是雙打。綁定引擎不處理隱式強制轉換。

將您的DP更改爲該類型,它應該工作正常。

+0

這樣做。謝謝! – Spontifixus 2013-02-21 10:06:23

相關問題