Discovering the ButtonBase
What do a Button, Checkbox, and RadioButton have in common? When you click on them something happens. This is enough to make them share a base class: the ButtonBase.
► This class defines one important event: the Click event. By handling this event, you define what the reaction of the application must be.
► ButtonBase derives from ContentControl. All subclasses get a Content property that is of type object. You can store anything in the Content property, for example, a string (which will become the text of the control), but also an image, any control, or even a panel containing other controls.
► You can use the property ClickMode to define when the Click event must be raised: Press raises the event when the mouse is pressed down, Release when it is not pressed anymore, and Hover when the mouse just passes over the control.
Other properties help you to find out what the state of the button is—for example, IsFocused, IsPressed, IsMouseOver. Like all the properties starting with the Is prefix, these are boolean values.
WARNING
You won't find these "Is" properties in the XAML editor. Why not? They are read-only, and cannot be set! You can only access them from the code-behind.
Button, HyperlinkButton, RepeatButton, and ToggleButton
Let's talk about four important controls deriving from ButtonBase and thus sharing its functionality:
► A Button is pretty obvious. Click it and something happens. To specify what, handle the Click event. It's as simple as can be.
► The HyperlinkButton looks just like an HTML hyperlink. You can also simulate this by using a TextBlock and handling its events. However, using the HyperlinkButton is
320 CHAPTER 16 Digging Deeper into Silverlight Elements actually a clever move. Since it's a Control, it has states (MouseOver, Unfocused, Focused, Pressed, and so on) for which you can easily create a different look and feel, for example, in Blend. We talk about that in later chapters.
► A RepeatButton also has a Click event. The difference is that, if the user holds down the mouse button, the Click event will be raised again in a rapid succession until the mouse button is released.
You can control the timing with the Delay (milliseconds before rapid succession starts) and the Interval property (milliseconds between two Click events).
► Finally, a ToggleButton is a button that switches between two states (toggled or untoggled) when you press it. The visual aspect of the button changes to notify the user. The state of the control is saved in the IsChecked property.
In fact, the ToggleButton has a 3rd state. If you look at the ToggleButton class in the SDK documentation, you will see that the property IsChecked is a nullable boolean (we talked about them briefly in Chapter 10, "Progressing with .NET"). It means that this value can be true, false or null. In that last case, the ToggleButton is said to be in indeterminate state.
CheckBox and RadioButton
CheckBox and RadioButton are derived from ToggleButton. Since these controls all switch between a toggled and untoggled state (and don't forget the indeterminate state), and since the only difference between the states is really just a matter of graphics design, it makes sense to reuse the capabilities of the ToggleButton.
► A CheckBox is really just a ToggleButton. It adds strictly no functionality; it just looks different.
► A RadioButton is similar to a CheckBox. There is one big difference though: The GroupName property. Setting this property adds the RadioButton to a group. Only one button in each group can be checked. Note that the buttons of the same group are mutually exclusive wherever they are placed in the user control. They don't need to be all in the same panel, for example.
Post a comment