Simple Custom Button : Button « Windows Presentation Foundation « VB.Net Tutorial






<Window x:Class="ControlTemplates.SimpleCustomButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SimpleCustomButton" Height="410" Width="400">
  <Window.Resources>
    <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
      <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2"
       Background="Red" TextBlock.Foreground="White" Name="Border">
        <Grid>
          <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black"
           StrokeThickness="1" StrokeDashArray="1 2"
           SnapsToDevicePixels="True" ></Rectangle>
          <ContentPresenter RecognizesAccessKey="True"
         Margin="{TemplateBinding Padding}"></ContentPresenter>
        </Grid>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter TargetName="Border" Property="Background" Value="Red" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
          <Setter TargetName="Border" Property="Background" Value="IndianRed" />
          <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="True">
          <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
          <Setter TargetName="Border" Property="TextBlock.Foreground" Value="Gray" />
          <Setter TargetName="Border" Property="Background" Value="MistyRose" />
        </Trigger>

      </ControlTemplate.Triggers>
    </ControlTemplate>

  </Window.Resources>
  <StackPanel Margin="10">
    <Button Template="{StaticResource ButtonTemplate}" Click="Clicked" Name="cmdOne">
      A Simple Button with a Custom Template
    </Button>
    <Button Template="{StaticResource ButtonTemplate}" IsEnabled="False" Click="Clicked" Name="cmdFour">
      A Disabled Button
    </Button>
  </StackPanel>
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes

Namespace ControlTemplates

  Public Partial Class SimpleCustomButton
    Inherits System.Windows.Window

    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub Clicked(sender As Object, e As RoutedEventArgs)
      MessageBox.Show("You clicked " & DirectCast(sender, Button).Name)
    End Sub

  End Class
End Namespace
WPF Simple Custom Button








16.6.Button
16.6.1.Rotated ButtonsRotated Buttons
16.6.2.Pendulum ButtonPendulum Button
16.6.3.Set Button Content with element valueSet Button Content with element value
16.6.4.Set Button Width,Height and Content String by using AttributesSet Button Width,Height and Content String by using Attributes
16.6.5.Use Button.Content to set the content for a ButtonUse Button.Content to set the content for a Button
16.6.6.Adding image to a ButtonAdding image to a Button
16.6.7.Button with no position and sizeButton with no position and size
16.6.8.Button with Property AttributesButton with Property Attributes
16.6.9.Button with Property ElementsButton with Property Elements
16.6.10.Button with default Xml namespaceButton with default Xml namespace
16.6.11.Button with named NamespaceButton with named Namespace
16.6.12.Button Based On StyleButton Based On Style
16.6.13.Property Trigger for ButtonProperty Trigger for Button
16.6.14.Layout TranformLayout Tranform
16.6.15.Using alternate elements as the content of a ButtonUsing alternate elements as the content of a Button
16.6.16.Button's Background = ImageBrush. The resulting button has an image as its backgroundButton's Background = ImageBrush. The resulting button has an image as its background
16.6.17.A rotated ButtonA rotated Button
16.6.18.Using Button.Content to set content for ButtonUsing Button.Content to set content for Button
16.6.19.A button that has been skewed and rotatedA button that has been skewed and rotated
16.6.20.Button PreviewMouseDown action and MouseDown actionButton PreviewMouseDown action and MouseDown action
16.6.21.Add CommandTarget for ButtonAdd CommandTarget for Button
16.6.22.Adding graphics to a ButtonAdding graphics to a Button
16.6.23.Button with image and text, use grid to layout Button contentButton with image and text, use grid to layout Button content
16.6.24.Canvas with Button onCanvas with Button on
16.6.25.Resizing ButtonsResizing Buttons
16.6.26.To add a button control and a text block to the canvasTo add a button control and a text block to the canvas
16.6.27.A simple template for a round buttonA simple template for a round button
16.6.28.Load style defined in Xaml and apply to the ButtonLoad style defined in Xaml and apply to the Button
16.6.29.Nested Button contentNested Button content
16.6.30.Two repeat buttons that increase and decrease a numerical value.Two repeat buttons that increase and decrease a numerical value.
16.6.31.Set button properties with Linq styleSet button properties with Linq style
16.6.32.Simple Custom ButtonSimple Custom Button
16.6.33.Button VerticalAlignment=Top HorizontalAlignment=LeftButton VerticalAlignment=Top  HorizontalAlignment=Left