C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: We want to make the Button do something when the user clicks on it. We must modify the XAML markup.
So: Near the end of the Button element, add the Click attribute. Visual Studio will offer the option to make a new event handler.
EventExample markup: XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="75"
Click="Button_Click"/>
</Grid>
</Window>
Example code: C#
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Title = "Clicked";
}
}
}
Here: We apply four different padding values to a button. Each side of the button (in the screenshot) has a different padding.
Example markup, Padding: XAML
<Window x:Class="WpfApplication15.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" HorizontalAlignment="Left"
Margin="10" VerticalAlignment="Top"
Padding="10,20,30,40"/>
</Grid>
</Window>
HorizontalAlignment: With the HorizontalAlignment and VerticalAlignment properties, we can anchor, center, or stretch Buttons.
IsEnabled: We use the IsEnabled property to disable a button. This makes it unusable.
IsEnabledToolTip: A Button can have a ToolTip. The ToolTip helps indicate what a control (like a Button) does.
ToolTip