#1,177 – UIElement vs. FrameworkElement vs. Control

If you want to implement a custom element in WPF, either to display something in a user interface or to get input from a user, you’ll typically derive your custom element from one of the following classes: UIElementFrameworkElement, or Control.

The inheritance chain for these three classes is:

1177-001

When creating a custom control, you’ll want to choose as a base class whichever one of these classes has only the features that you want.  The core functionality for these three classes is:

  • UIElement  (Layout + Input + Focus + Events)
    • Layout behavior (parent/child relationship, measure/arrange passes)
    • Responding to user input (input events, command bindings)
    • Managing focus
    • Raising and responding to routed events
  • FrameworkElement adds
    • Alignment-related and Margin properties
    • Animation support
    • Data binding
    • Data templates
    • Styles
    • Defaults Focusable to false
  • Control adds
    • Control templates
    • Background, Foreground
    • Font-related properties
    • Border-related properties
    • Defaults Focusable to true
Advertisement

#296 – Controls that Derive from the Control Class

The Control class in WPF inherits from FrameworkElement and is itself the base class for controls that a user interacts with.

Some of the more common controls that derive from Control are:

  • Calendar – select a date
  • Expander – expand/collapse other controls
  • GroupBox – groups other controls
  • Label – displays some text
  • Button – click to initiate an action
  • RepeatButton – click repeatedly
  • CheckBox – turn something on/off
  • RadioButton – select one item from a group
  • DatePicker – select a date
  • ContextMenu – displays context-sensitive menu
  • Menu – select a command from a menu
  • ComboBox – select an item from a dropdown list
  • ListBox – select an item from a list
  • DataGrid – view/edit data in a grid
  • TabControl – displays a set of tabs, each containing child controls
  • StatusBar – shows information in horizontal bar
  • TreeView – displays hierarchical view
  • PasswordBox – enter a password
  • ProgressBar – shows progress of ongoing operation
  • Slider – select from a range of values
  • RichTextBox – edit a formatted document
  • TextBox – enter/edit some text

#207 – Setting a Control’s Background Color

The Control class is a base class for all user interface elements that a user interacts with.  (This includes the Window class).

Control includes a Background property that you can set to cause the background of the control to be filled in, or colored.

Background’s type is System.Windows.Media.Brush, which means that you specify not just a background color, but an instance of a Brush object.

There are several kinds of brushes, but the most common is the SolidColorBrush, which allows setting the background to a solid color.

In the example below, we set the background color of a Window to the color PowderBlue.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Background>
        <SolidColorBrush Color="PowderBlue"/>
    </Window.Background>
    <Grid>
        <Label Content="Hi!"/>
    </Grid>
</Window>

Here’s the result:

Here are some other examples of setting the Background property for various controls:

#33 – Control Class

The Control class inherits from FrameworkElement and is the base class for all controls that a user interacts with.  Examples of controls include: TextBox, Label, ListBox, ComboBox and Button.  Container elements like Panel and Grid are not controls.

The Control class extends FrameworkElement by adding functionality to:

  • Change the control’s visual appearance through the use of templates
  • Support visual elements like fonts, borders and a background color