Showing posts with label Control. Show all posts
Showing posts with label Control. Show all posts

Friday, March 11, 2011

GroupBox

Traditionally, a group of mutually exclusive radio buttons are children of a group box, which is a control with a simple outline border and a text heading. In the Windows Presentation Foundation, this is the GroupBox control, which is one of three classes that descend from HeaderedContentControl:

Control
---ContentControl
------HeaderedContentControl
---------Expander
---------GroupBox
---------TabItem

Because HeaderedContentControl derives from ContentControl, these controls have a Content property. The controls also have a Header property, which (like Content) is of type object. GroupBox adds nothing to HeaderedContentControl. Use the Header property to set the heading at the top of the GroupBox. Although this heading is customarily text, you can really make it whatever you want. Use Content for the interior of the GroupBox. You'll probably want to put a StackPanel in there.

//IN WINDOWS CONSTRUCTOR
{
Title = "Tune the Radio";
SizeToContent = SizeToContent.WindowandHeight;

GroupBox group = new GroupBox();
group.Header = "Window Style";
group.Margin = new Thickness(96);
group.Padding = new Thickness(5);
Content = group;
//
StackPanel stack = new StackPanel();
group.Content = stack;
//
stack.Children.Add( CreateRadioButton("No border or caption",
WindowStyle.None));
stack.Children.Add( CreateRadioButton("Single-border window",
WindowStyle.SingleBorderWindow));
stack.Children.Add(CreateRadioButton("3D-border window",
WindowStyle.ThreeDBorderWindow));
stack.Children.Add( CreateRadioButton("Tool window",
WindowStyle.ToolWindow));
//
AddHandler(RadioButton.CheckedEvent, new RoutedEventHandler(
RadioOnChecked));

}//End THE CONSTRUCTOR
RadioButton CreateRadioButton(string strText, WindowStyle winstyle)
{
RadioButton radio = new RadioButton();
radio.Content = strText;
radio.Tag = winstyle;
radio.Margin = new Thickness(5);
radio.IsChecked = (winstyle ==WindowStyle);

return radio;
}
//
void RadioOnChecked(object sender,RoutedEventArgs args)
{
RadioButton radio = args.Source as RadioButton;
WindowStyle = (WindowStyle)radio.Tag;
}


The checking and unchecking of sibling radio buttons happens automatically from either the mouse or keyboard. When you're using the keyboard, the cursor movement keys change the input focus among the sibling radio buttons. The space bar checks the currently focused button. All the program needs to do is monitor which button is being checked. (Some programs might also need to know when a button is losing its check mark.) It is usually most convenient to use the same event handler for sibling radio buttons. When writing code for the event handler, keep in mind that the IsChecked property has already been set.

It could be that you have a bunch of sibling radio buttons that comprise two or more mutually exclusive groups. To distinguish between these groups, use the GroupName property defined by RadioButton. Set GroupName to a unique string for each group of mutually exclusive buttons, and the multiple groups will be automatically checked and unchecked correctly. You can use the same GroupName property in your event handler to determine which group of buttons is affected.

Tuesday, March 8, 2011

Controls in WPF (Code)

In the Windows Presentation Foundation, the term control is somewhat more specialized than in earlier Windows programming interfaces.

In Windows Forms, for example, everything that appears on the screen is considered a control of some sort. In the WPF, the term is reserved for elements that are user interactive, which means that they generally provide some kind of feedback to the user when they are prodded with the mouse or triggered by a keystroke. The TextBlock, Image, and Shape elements all receive keyboard, mouse, and stylus input, but they choose to ignore it. Controls actively monitor and process user input.

The Control class descends directly from FrameworkElement:

Object
--DispatcherObject (abstract)
------DependencyObject
--------Visual (abstract)
------------UIElement
------------------FrameworkElement
------------------------Control

Window derives from Control by way of ContentControl. Properties defined by Control include Background, Foreground, BorderBrush, BorderThickness, and font-related properties, such as FontWeight and FontStretch. (Although TextBlock also has a bunch of font properties, TextBlock does not derive from Control. TextBlock defines those properties itself.)

From Control descend more than 50 other classes, providing programmers with favorites such as buttons, list boxes, scroll bars, edit fields, menus, and toolbars. The classes implementing these controls can all be found in the System.Windows.Controls and System.Windows.Controls.Primitives namespaces, along with other classes that do not derive from Control.