Showing posts with label Content. Show all posts
Showing posts with label Content. 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

The concept of Content

The Window class has more than 100 public properties, and some of themsuch as the Title property that identifies the windoware quite important. But by far the most important property of Window is the Content property. 
You set the Content property of the window to the object you want in the window's client area.
You can set the Content property to a string, you can set it to a bitmap, you can set it to a drawing, and you can set it to a button, or a scrollbar, or any one of 50-odd controls supported by the Windows Presentation Foundation. 
You can set the Content property to just about anything. But there's only one little problem:
You can only set the Content property to one object.
This restriction is apt to be a bit frustrating in the early stages of working with content. Eventually, of course, you'll see how to set the Content property to an object that can play host to multiple other objects. For now, working with a single content object will keep us busy enough.


We've seen that window content can be plain text. But the purpose of the Content property is not to display simple unformatted text. No, no, no, no, no. What the Content property really wants is something more graphical in nature, and that's an instance of any class that derives from UIElement.
UIElement is an extremely important class in the Windows Presentation Foundation. This is the class that implements keyboard, mouse, and stylus handling. The UIElement class also contains an important method named OnRender. The OnRender method is invoked to obtain the graphical representation of an object.
As far as the Content property goes, the world is divided into two groups of objects: Those that derive from UIElement and those that do not. In the latter group, the object is displayed with ToString; in the former group, it's displayed with OnRender. Classes that derive from UIElement (and their instantiated objects) are often referred to collectively as elements.
The only class that inherits directly from UIElement is FrameworkElement, and all the elements you'll encounter in the Windows Presentation Foundation derive from FrameworkElement. In theory, UIElement provides the necessary structure of user interface events and screen rendering that can support various programming frameworks. The Windows Presentation Foundation is one such framework and consists of all the classes that derive from FrameworkElement. In a practical sense, you probably won't make much of a distinction between the properties, methods, and events defined by UIElement and those defined by FrameworkElement.
 
One popular class that inherits from FrameworkElement is Image. Here's the class hierarchy:
Object
--DispatcherObject (abstract)
------DependencyObject
----------Visual (abstract)
------------------UIElement
----------------------FrameworkElement
----------------------------Image
The Image class lets you easily include an image in a document or an application.