<Label Grid.Row="1" > Enter your last name: </Label> <TextBox Grid.Row="1" Grid.Column="1" Name="lastName" Margin="0,5,10,5"/>
<Button Grid.Row="2" Grid.Column="0" Name="submit" Margin="2"> View message </Button>
<Button Grid.Row="2" Grid.Column="1" Name="Clear" Margin="2"> Clear Name </Button> </Grid>
<Button FontSize="14" FontWeight="Bold"> <!--Set the Background property of the Button to a LinearGradientBrush.--> <Button.Background> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Color="Green" Offset="0.0" /> <GradientStop Color="White" Offset="0.9" /> </LinearGradientBrush>
</Button.Background> View message </Button> The following example sets the same properties in code.
</Setter.Value> </Setter> </Style>
<!--Define a template that creates a gradient-colored button.--> <Style TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border x:Name="Border" CornerRadius="20" BorderThickness="1" BorderBrush="Black"> <Border.Background> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Color="{Binding Background.Color, RelativeSource={RelativeSource TemplatedParent}}" Offset="0.0" /> <GradientStop Color="White" Offset="0.9" /> </LinearGradientBrush> </Border.Background> <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/> </Border> <ControlTemplate.Triggers> <!--Change the appearance of the button when the user clicks it.--> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="Border" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Color="{Binding Background.Color, RelativeSource={RelativeSource TemplatedParent}}" Offset="0.0" /> <GradientStop Color="DarkSlateGray" Offset="0.9" /> </LinearGradientBrush> </Setter.Value> </Setter> </Trigger>
</ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
<Button Grid.Row="2" Grid.ColumnSpan="2" Name="submitName" Background="Green">View message</Button> Note: The Background property of the Button must be set to a SolidColorBrush for the example to work properly. Subscribing to Events You can subscribe to a control's event by using either XAML or code, but you can only handle an event in code. The following example shows how to subscribe to the Click event of a Button.
For more information about these classes, see Controls Content Model Overview.