The following example shows how to add a custom style as a StaticResource. For more information, see Styling and Templating Overview. In this example, the StaticResource refers to an x:Key attribute value that you must add to the Style element when you modify it for your project.
|
<HyperlinkButton x:Name="hyperlinkButton1"
Style="{StaticResource newHyperlinkButtonStyle}" />
|
The following example shows the default styles and templates for the HyperlinkButton control. To customize these styles, add the following XAML to your project, add an x:Key attribute to the Style element, and then reference the style as shown in the previous example.
|
<Style TargetType="HyperlinkButton" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Setter Property="IsEnabled" Value="true" />
<Setter Property="IsTabStop" Value="true" />
<Setter Property="Foreground" Value="#FF417DA5" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="TextAlignment" Value="Left" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="TextWrapping" Value="NoWrap" />
<!-- Cannot currently parse TextDecorationCollection type in XAML so it's being set in code -->
<!-- <Setter Property="TextDecorations" Value="Underline" /> -->
<!-- Cannot currently parse FontFamily type in XAML so it's being set in code -->
<!-- <Setter Property="FontFamily" Value="Trebuchet MS" /> -->
<Setter Property="FontSize" Value="11" />
<!-- Cannot currently parse FontWeight type in XAML so it's being set in code -->
<!-- <Setter Property="FontWeight" Value="Bold" /> -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HyperlinkButton">
<Grid Cursor="{TemplateBinding Cursor}">
<Grid.Resources>
<!-- Visual constants used by the template -->
<Color x:Key="FocusColor">#FF333333</Color>
<SolidColorBrush x:Name="HoverColor" Color="#FF99C3F7"/>
</Grid.Resources>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CommonStates">
<vsm:VisualState x:Name="Normal" />
</vsm:VisualStateGroup>
<vsm:VisualStateGroup x:Name="FocusStates">
<vsm:VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="Visibility" Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Unfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="Visibility" Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<!-- Focus indicator -->
<Rectangle x:Name="FocusVisual" StrokeDashCap="Round" StrokeDashArray=".2 5" Visibility="Collapsed" IsHitTestVisible="false">
<Rectangle.Stroke>
<SolidColorBrush Color="{StaticResource FocusColor}" />
</Rectangle.Stroke>
</Rectangle>
<!-- HyperlinkButton content -->
<ContentPresenter
x:Name="Content"
Background="{TemplateBinding Background}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
TextAlignment="{TemplateBinding TextAlignment}"
TextDecorations="{TemplateBinding TextDecorations}"
TextWrapping="{TemplateBinding TextWrapping}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="0,-2,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
|