05 January 2011

XAML - Custom button style

An example style for a custom button to put in a resource dictionary:

Silverlight

<Style TargetType="Button">
  <Setter Property="Cursor" Value="Hand" />
  <Setter Property="Margin" Value="2" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="Common">
              <VisualState Name="Normal">
                <Storyboard>
                  <ColorAnimation Storyboard.TargetName="objBackgroundTop" Storyboard.TargetProperty="Color" Duration="0:0:0.2" From="Green" To="#EEEEEE" />
                </Storyboard>
              </VisualState>
              <VisualState Name="MouseOver">
                <Storyboard>
                  <ColorAnimation Storyboard.TargetName="objBackgroundTop" Storyboard.TargetProperty="Color" Duration="0:0:0.2" From="#EEEEEE" To="Green" />
                </Storyboard>
              </VisualState>
              <VisualState Name="Pressed">
                <Storyboard>
                  <ColorAnimation Storyboard.TargetName="objBackgroundTop" Storyboard.TargetProperty="Color" Duration="0:0:0.2" From="Green" To="CornflowerBlue" />
                </Storyboard>
              </VisualState>
              <VisualState Name="Disabled">
                <Storyboard>
                  <ColorAnimation Storyboard.TargetName="objBackgroundTop" Storyboard.TargetProperty="Color" Duration="0:0:0.2" From="#EEEEEE" To="#444444" />
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Rectangle RadiusX="5" RadiusY="5">
            <Rectangle.Fill>
              <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Offset="0" Color="#EEEEEE" x:Name="objBackgroundTop" />
                <GradientStop Offset="1" Color="#111111" />
              </LinearGradientBrush>
            </Rectangle.Fill>
          </Rectangle>
          <ContentPresenter />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

WPF

Another example for WPF. Here I used triggers and resources:

<Style x:Key="Button" TargetType="Button">

  <Style.Resources>

    <LinearGradientBrush x:Key="lgbNormal" StartPoint="0,0" EndPoint="0,1">

      <GradientStop Offset="0" Color="{x:Static SystemColors.ControlLightLightColor}" />

      <GradientStop Offset="0.5" Color="{x:Static SystemColors.ControlLightColor}" />

      <GradientStop Offset="0.5" Color="{x:Static SystemColors.ControlDarkDarkColor}" />

      <GradientStop Offset="1" Color="{x:Static SystemColors.ControlDarkDarkColor}" />

    </LinearGradientBrush>

    <LinearGradientBrush x:Key="lgbMouseOver" StartPoint="0,0" EndPoint="0,1">

      <GradientStop Offset="0" Color="{x:Static SystemColors.ControlLightLightColor}" />

      <GradientStop Offset="0.5" Color="#083B80" />

      <GradientStop Offset="0.5" Color="{x:Static SystemColors.ControlDarkDarkColor}" />

      <GradientStop Offset="1" Color="{x:Static SystemColors.ControlDarkDarkColor}" />

    </LinearGradientBrush>

  </Style.Resources>

  <Setter Property="Template">

    <Setter.Value>

      <ControlTemplate TargetType="Button">

        <Border x:Name="border" BorderThickness="1" BorderBrush="#3E3E3E" CornerRadius="2">

          <Grid>

            <Rectangle x:Name="rect" RadiusX="2" RadiusY="2" Fill="{StaticResource lgbNormal}" />

            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />

          </Grid>

        </Border>

        <ControlTemplate.Triggers>

          <Trigger Property="IsMouseOver" Value="True">

            <Setter TargetName="rect" Property="Fill" Value="{StaticResource lgbMouseOver}" />

            <Setter TargetName="border" Property="BorderBrush" Value="#083B80" />

          </Trigger>

          <Trigger Property="IsEnabled" Value="False">

            <Setter TargetName="border" Property="Opacity" Value="0.5" />

          </Trigger>

        </ControlTemplate.Triggers>

      </ControlTemplate>

    </Setter.Value>

  </Setter>

</Style>

No comments:

Post a Comment