我目前有一个绑定到ObservableCollection的组合框
<ComboBox ItemsSource="{Binding Past}" DisplayMemberPath="Date" IsSynchronizedWithCurrentItem="True"/>
使用'IsSynchronizedWithCurrentItem'它与一组标签“同步”,这些标签在一组标签中显示以下数据:
<Label DataContext="{Binding SelectedDate}" Content="{Binding Minimum}" />
因为使用DatePicker(比如WPF Toolkit one,http://wpf.codeplex.com/)而不是其中包含超过300个日期的组合框来选择日期要容易得多,所以在某种程度上设置类似'IsSynchronizedWithCurrentItem'的东西以便DatePicker可以控制'当前日期'?
谢谢
答案 0 :(得分:1)
我通过在View模型中创建'CurrentDate'属性来解决这个问题:
public DateTime CurrentDate
{
get { return (this.collectionView.CurrentItem as PastItem).Date; }
set
{
this.collectionView.MoveCurrentTo(Past.Where(PastItem => PastItem.Date == Convert.ToDateTime(value)).FirstOrDefault());
}
}
然后为第一个&amp;创建两个属性最后的日期:
public DateTime FirstDate
{
get { return this.Past.FirstOrDefault().Date; }
}
public DateTime LastDate
{
get { return this.Past.LastOrDefault().Date; }
}
然后使用DatePicker绑定到这些属性:
<wpf:DatePicker SelectedDate="{Binding CurrentDate}" DisplayDateStart="{Binding FirstDate, Mode=OneWay}" DisplayDateEnd="{Binding LastDate, Mode=OneWay}" />
这意味着DatePicker将仅限于第一个&amp;最后的日期,可以选择与详细信息相关联的日期。