views:

128

answers:

1

I currently have a combobox with bound to an ObservableCollection

    <ComboBox ItemsSource="{Binding Past}" DisplayMemberPath="Date" IsSynchronizedWithCurrentItem="True"/>

Using, 'IsSynchronizedWithCurrentItem' it "synchronizes" with a set of labels that show the data below in a set of labels like:

    <Label DataContext="{Binding SelectedDate}" Content="{Binding Minimum}" />

As it is a lot easier to select a date using a DatePicker (like the WPF Toolkit one, http://wpf.codeplex.com/) rather than a combobox with over 300 dates in it, is there someway to set something like 'IsSynchronizedWithCurrentItem' so that the DatePicker can control the 'current date'?

Thank you

A: 

I solved this by creating a 'CurrentDate' Property in my View Model:

    public DateTime CurrentDate
    {
        get { return (this.collectionView.CurrentItem as PastItem).Date; }
        set 
        { 
            this.collectionView.MoveCurrentTo(Past.Where(PastItem => PastItem.Date == Convert.ToDateTime(value)).FirstOrDefault()); 
        }
    }

and then creating two property's for the first & the last dates:

    public DateTime FirstDate
    {
        get { return this.Past.FirstOrDefault().Date; }
    }
    public DateTime LastDate
    {
        get { return this.Past.LastOrDefault().Date; }
    }

and then binding to these properties using the DatePicker:

    <wpf:DatePicker SelectedDate="{Binding CurrentDate}" DisplayDateStart="{Binding FirstDate, Mode=OneWay}" DisplayDateEnd="{Binding LastDate, Mode=OneWay}" />

This meant that the DatePicker would be limited to the first & the last dates, and one can choose a date, linked to the details.