DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML和Xhtml
XAML 中把 Button 重繪成圓形的技巧
編輯:HTML和Xhtml     

用 XAML 排版時,為了讓介面更貼近 Metro 風格,有些按鈕需要從預設的長方形改成圓形。以下這段 Style 可以直接解決,也能依需求再自行調整。當然,如果你熟悉 Blend,用它拉出想要的樣式會更直觀——但直接貼程式碼,速度是不是更快一點呢?

<Style x:Key="btnNext" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="rectangle">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Thickness>-3</Thickness>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="MouseOver"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" RadiusY="25" RadiusX="25" Stroke="Blue" StrokeThickness="4">
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Margin" Value="5"/>
<Setter Property="Width" Value="50"/>
<Setter Property="Height" Value="50"/>
<Setter Property="FontSize" Value="120"/>
<Setter Property="Foreground" Value="White"/>
</Style>

關鍵在 RadiusX 與 RadiusY:當按鈕是 50×50,而圓角半徑設為 25,等於取邊長的一半,Rectangle 就會被裁成一個正圓。ContentPresenter 則負責把按鈕原本的內容(文字或圖示)畫在形狀之上,並沿用 TemplateBinding 繼承對齊設定。

若之後想改成膠囊形,只要把 RadiusY 調小、RadiusX 維持較大值即可;想改填色,把 Rectangle 加上 Fill 或換成 Ellipse 都是可行方向。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved