In Focus

Button Control In Universal Apps

In this article, I'll be talking about using Button control and declaring it in an example.

Button control is the most classic control that many developers and users are aware of. It mainly executes a command once it is clicked.

Here's how it looks like:


Here's how to declare it in XAML:
  1. <Button x:Name="button" Content="Click Me!" HorizontalAlignment="Left" Margin="70,98,0,0" VerticalAlignment="Top"/>  
Buttons have Click event for doing some work when clicked. Here's how we declared it in our sample:
  1. <Button x:Name="button" Content="Click Me!" Width="80" Height="32" Click="button_Click"/>  
  1. public async void ShowMessage(string message)  
  2. {  
  3.    var msg = new Windows.UI.Popups.MessageDialog(message);  
  4.    msg.DefaultCommandIndex = 1;  
  5.    await msg.ShowAsync();  
  6. }  
  7.   
  8. private void button_Click(object sender, RoutedEventArgs e)  
  9. {  
  10.    ShowMessage("Hello World");  
  11. }  



Buttons also have some properties you need to be aware of.

ClickMode

This property helps you choose from 3 types of Click once you set: Release, Press and Hover.

Release click event executes after you clicked but not ended mouseclick or tapping.

Press click event executes same time you click a Button control.

Hover executes immediately when you get your mouse icon on the button or tap it. Thinking of touch screens, Hover click isn't for them.
 
Here's an example of using Press ClickMode:
  1. <Button x:Name="button" Content="Click Me!" ClickMode="Press"/>   
RequestedTheme:
 
Let's say you decided to use Light Theme for your app. All the other controls inherit that option and use the same theme of your app.
But you can manually change Button's Theme using this property.

Here's an example,

Light Theme
  1. <Button x:Name="button" Content="Click Me!" RequestedTheme="Light"/>  
Dark Theme
  1. <Button x:Name="button" Content="Click Me!" RequestedTheme="Dark"/>  
Default Theme(Inherit)
  1. <Button x:Name="button" Content="Click Me!" HorizontalAlignment="Left" Margin="70,98,0,0" VerticalAlignment="Top" AllowDrop="True" ClickMode="Press" RequestedTheme="Default"/>  
BorderThickness

You can also set how thick a Button can be.

Here's an example with Thickness set"0": 
  1. <Button x:Name="button" Content="Click Me!" BorderThickness="0" Width="80" Height="32"/>   
Buttons are the best way to get interacted with user as it is used in many apps to do some background task.