2011-04-15 167 views
2

標題有點模糊,問題是:將模板屬性值綁定到模板控件屬性

我正在實現一個Silverlight 4按鈕,通過交換模板和我自己的。是否可以將邊框圓角半徑綁定到按鈕高度?

例如,用戶可以設置高度設計師爲30,則該模板內邊界的角部半徑應15.當高度爲50,則角半徑應爲25,等

如果可能的,我需要一個純粹的XAML解決方案。

謝謝

回答

3

這是沒有純粹的Xaml解決方案。最終,您至少需要執行y/2表達式,而這不是現有基於Xaml的組件提供的內容。

在Vs2010中打開項目。添加一個新項目...「Silverlight模板控制」稱它爲「RoundEndedButton」。

與更換來源: -

public class RoundEndedButton : Button 
{ 
    public RoundEndedButton() 
    { 
     this.DefaultStyleKey = typeof(RoundEndedButton); 
     SizeChanged += new SizeChangedEventHandler(RoundEndedButton_SizeChanged); 
    } 

    public static readonly DependencyProperty CornerRadiusProperty = 
     DependencyProperty.Register(
      "CornerRadius", 
      typeof(CornerRadius), 
      typeof(RoundEndedButton), 
      new PropertyMetadata(new CornerRadius())); 

    void RoundEndedButton_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     SetValue(CornerRadiusProperty, new CornerRadius(e.NewSize.Height/2)); 
    } 

} 

在主題/ Generic.xaml修改其默認模板。這裏是我非常簡單的例子: -

<Style TargetType="local:RoundEndedButton"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:RoundEndedButton"> 
       <Border x:Name="Background" 
         Background="{TemplateBinding Background}" 
         CornerRadius="{TemplateBinding CornerRadius}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         BorderBrush="{TemplateBinding BorderBrush}"> 
        <ContentPresenter 
         x:Name="contentPresenter" 
         Content="{TemplateBinding Content}" 
         ContentTemplate="{TemplateBinding ContentTemplate}" 
         VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
         HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
         Margin="{TemplateBinding Padding}"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

注意在模板綁定中使用額外的CornerRadius屬性。當然,現在您可以切換回混合模式,將此控件添加到曲面並在樣式上獲得創意。