Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Who’s the Boss

0.00/5 (No votes)
5 Sep 2010CPOL2 min read 9.3K  
Who's the boss?

“With great power comes great responsibility” - Uncle Ben (Spider-Man)

In the previous article, I covered the TimeslotPanel… This panel is VERY flexible and allows us to arrange items based on timeslots. This begs the question… Who controls this panel?

Design a DateRangeSpinner

As a control, the DateRangeSpinner is VERY simple. It has 2 buttons (Next and Previous) that will adjust the DateRange based on the selected constraint (Day = 1, Week = 7, etc.) and a display text that should show the current DateRange in a human readable format. To use the DateRangeSpinner is very easy, first create a spinner:

XML
<rg:DateRangeSpinner x:Name="dateRangeSpinner" DateRangeConstraints="Week" />

And then to use it:

XML
<rg:TimeslotPanel ShowGridLines="True" 
      DateRange="{Binding ElementName=dateRangeSpinner, Path=DateRange, Mode=TwoWay}">
            
</rg:TimeslotPanel>

As the range changes, the TimeslotPanel will re-adjust!!!

What Have I Learned?

Why Not Derive from Spinner?

The Silverlight toolkit has a cool base control called a Spinner! It would have made sense to derive from this control (and in the future I might) but for now I did not want to force a dependency on any external libraries (especially because I am targeting both WPF and Silverlight).

Theme-ability?

One of my design goals was theme-ability… Both the previous and next buttons are style-able.

XML
<rg:DateRangeSpinner>
    <rg:DateRangeSpinner.PreviousButtonStyle>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </rg:DateRangeSpinner.PreviousButtonStyle>
</rg:DateRangeSpinner>

Making WPF Controls Work in Silverlight

By default, a new control created using the WPF template has a static constructor:

C#
static DateRangeSpinner()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(DateRangeSpinner), 
	new FrameworkPropertyMetadata(typeof(DateRangeSpinner)));
}

“The DefaultStyleKeyProperty, as the name suggests, is used to tell WPF about the key that should be used to perform the style lookup. By overriding this property as shown in the preceding example, you are telling WPF to use the control’s type as the key for its default style. If you skip this step, WPF will have no way to discover the default style of the control (defined in generic.xaml in the control’s assembly).”

To allow this code to work in Silverlight, change it to the following:

C#
public DateRangeSpinner()
{
    DefaultStyleKey = typeof(DateRangeSpinner);
}

Also Read

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)