#316 – Changing the ClickMode of a Button

A standard Button in WPF (and in Windows in general) performs some action when you click it.  Traditionally “click it” has meant “depress and then release the left mouse button”.  The action associated with the button typically occurs only on the release of the button.

In WPF, you can change when the button fires its Click event by using the Button.ClickMode property.

  • ClickMode.Release – fires when you release the mouse button  (Default)
  • ClickMode.Press – fires when you press the mouse button
  • ClickMode.Hover – fires when you hover over the button
<StackPanel>
    <Button Content="Release" ClickMode="Release" Height="30" Width="100" Margin="15" Click="Button_Click"/>
    <Button Content="Press" ClickMode="Press" Height="30" Width="100" Margin="15" Click="Button_Click"/>
    <Button Content="Hover" ClickMode="Hover" Height="30" Width="100" Margin="15" Click="Button_Click"/>
    <Label Name="lblInfo" Margin="15"/>
</StackPanel>

 

        private int clickNum = 1;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;

            lblInfo.Content = string.Format("Click {0}, Mode {1}", clickNum++, b.ClickMode.ToString());
        }

Advertisements