Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Who’s the Boss

, 5 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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?

“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:

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

And then to use it:

<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.

<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:

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:

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)

Share

About the Author

rudigrobler

South Africa South Africa
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 6 Sep 2010
Article Copyright 2010 by rudigrobler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid