views:

202

answers:

2

We have a report that compares data between weeks, and it seems like this is the exception and not the rule in the reporting world.

I'm looking for an elegant way of selecting "a week" in a Silverlight control. I'd prefer not to spend the time building it ourselves, so it would be nice if I could buy a well polished control that does this.

Sure, I could use a drop down list, but I would like a way to easily navigate through potentially years worth of weeks. Any existing controls out there? Any clever ways of using basic controls to achieve our goal?

Thank you!

A: 

No, there isn't one available in the Silverlight SDK, Toolkit, or even one of the large third party vendors I looked at.

Jeff Wilcox
A: 

I don't know of any existing controls. However you could try using a calendar control with a behaviour to translate a selection into a range selection of of the entire week.

For example:

<sdk:Calendar HorizontalAlignment="Left"
              Margin="8,8,0,120"
              Width="187">
    <i:Interaction.Behaviors>
        <local:SelectWeekBehaviour />
    </i:Interaction.Behaviors>
</sdk:Calendar>

With the behaviour being:

public class SelectWeekBehaviour : Behavior<Calendar>
{
    private void DateChanged(object sender, SelectionChangedEventArgs e)
    {
        UnsubscribeFromSelectionNotifications();

        var selectedDate = AssociatedObject.SelectedDate;

        if (selectedDate.HasValue)
        {
            var dayOfWeek = (int)selectedDate.Value.DayOfWeek;

            var firstDate = selectedDate.Value.AddDays(-dayOfWeek);
            var lastDate = selectedDate.Value.AddDays(6 - dayOfWeek);

            AssociatedObject.SelectedDates.Clear();
            AssociatedObject.SelectedDates.AddRange(firstDate, lastDate);
        }

        SubscribeToSelectionNotifications();
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.SelectionMode = CalendarSelectionMode.SingleRange;
        AssociatedObject.FirstDayOfWeek = DayOfWeek.Sunday;

        SubscribeToSelectionNotifications();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        UnsubscribeFromSelectionNotifications();
    }

    private void SubscribeToSelectionNotifications()
    {
        AssociatedObject.SelectedDatesChanged += DateChanged;
    }

    private void UnsubscribeFromSelectionNotifications()
    {
        AssociatedObject.SelectedDatesChanged -= DateChanged;
    }
}

The behaviour is not quite production ready since I forced the week to start on sunday to make easy to figure out the start and end of the week.

Graeme Bradbury