Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / WPF

Dynamic Button Control (C# code or XAML code)

Rate me:
Please Sign up or sign in to vote.
2.63/5 (6 votes)
8 Jun 2009CPOL3 min read 63.6K   3.3K   37  
Dynamic Button Control (C# code or XAML code)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="SharedResources.xaml" />
  </ResourceDictionary.MergedDictionaries>

  <Style x:Key="GlassButton" TargetType="{x:Type Button}">
    <Setter Property="Margin" Value="1" />
    <Setter Property="SnapsToDevicePixels" Value="true"> </Setter>
    <Setter Property="OverridesDefaultStyle" Value="true"> </Setter>
    <Setter Property="MinHeight" Value="16"> </Setter>
    <Setter Property="MinWidth" Value="16"> </Setter>
    <Setter Property="FontFamily" Value="Verdana"> </Setter>
    <Setter Property="FontSize" Value="11px" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="Button" >
            <Grid x:Name="Content" RenderTransformOrigin="0.5,0.5">
            <Border Name="Outline" CornerRadius="9" Background="{StaticResource ButtonPushedFill}"
               BorderBrush="{StaticResource BorderStroke}"
               BorderThickness="1" />
              <!-- Background -->
            <Rectangle x:Name="GlassBackground" Margin="1,1,2,2" RadiusX="9" RadiusY="9"
             Fill="{StaticResource GlassBackgroundFill}">
            </Rectangle>
            <Rectangle x:Name="Highlight" Margin="1,1,2,2" RadiusX="9"
             RadiusY="9" Opacity="1" Fill="{StaticResource HighlightFill}">
               </Rectangle>
            <!-- This aligns the text in the center of the button -->
            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>
      
            <!-- Triggers -->
            <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="true">
                  <Setter TargetName="GlassBackground" Property="Fill" Value="{StaticResource HoverGlassBackgroundFill}" />
               </Trigger>
               <Trigger Property="IsPressed" Value="true">
                  <Setter TargetName="GlassBackground" Property="Fill" Value="{StaticResource ButtonPushedFill}" />
               </Trigger>
             
            </ControlTemplate.Triggers>

           </ControlTemplate>
          </Setter.Value>
          </Setter>
          </Style>
 
</ResourceDictionary>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions