[英]C#/WPF - How to enable only a specific set of dates(not a range) in a DatePicker object
I have a list of dates I would like the user to select from. 我有一个我希望用户选择的日期列表。 These dates are not sequentially right after each other and can be scattered thought a given period of time.
这些日期并不是相继排列的,并且可以在给定的时间段内分散。 Because of the dates being in a scattered nature, I can not use the "DisplayDateStart" and "DisplayDateEnd" properties of the WPF "DatePicker" control.
由于日期是分散的,我不能使用WPF“DatePicker”控件的“DisplayDateStart”和“DisplayDateEnd”属性。 Is there a way I can make the date picker grey out all other dates that are not in a given list.
有没有办法让日期选择器变灰显示给定列表中没有的所有其他日期。 Below is snippet of "pseudo" code demonstrating what I would like to accomplish.
下面是“伪”代码片段,展示了我想要完成的任务。
public void foo(List<DateTime> ldtData, DatePicker dpPicker) {
dpPicker.EnabledDates = ldtData; // What I would like to do in a perfect world (:
}
There is no EnabledDates
property. 没有
EnabledDates
属性。 There is a BlackoutDates property to which you can add dates that are not selectable. 有一个BlackoutDates属性,您可以添加不可选择的日期。
But if you bind to a list of selectable dates, you could use a CalendarDayButtonStyle
and a converter to determine whether a particular date should be selectable: 但是,如果绑定到可选日期列表,则可以使用
CalendarDayButtonStyle
和转换器来确定是否应该选择特定日期:
public class DateToBoolConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length < 2)
return true;
IList<DateTime> selectableDates = values[0] as IList<DateTime>;
if (selectableDates == null)
return true;
DateTime currentDate = (DateTime)values[1];
return selectableDates.Contains(currentDate);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
XAML: XAML:
<DatePicker x:Name="dpPicker">
<DatePicker.CalendarStyle>
<Style TargetType="Calendar">
<Setter Property="CalendarDayButtonStyle">
<Setter.Value>
<Style TargetType="CalendarDayButton">
<Style.Resources>
<local:DateToBoolConverter x:Key="DateToBoolConverter" />
</Style.Resources>
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="Opacity" Value="0.5" />
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource DateToBoolConverter}">
<Binding Path="Tag" RelativeSource="{RelativeSource AncestorType=DatePicker}" />
<Binding Path="." />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="IsHitTestVisible" Value="True" />
<Setter Property="Opacity" Value="1" />
</DataTrigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
</DatePicker.CalendarStyle>
</DatePicker>
Sample usage: 样品用法:
public void foo(List<DateTime> ldtData, DatePicker dpPicker)
{
dpPicker.Tag = ldtData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.