在没有最小和最大日期范围的所有月份和年份中禁用Wpf日期选择器中的特定日期

本文关键字:日期 选择器 Wpf 范围 | 更新日期: 2023-09-27 18:09:47

`<DatePicker x:Name="dpFrom"  CalendarOpened="dpFrom_CalendarOpened_1"/>
 <CheckBox x:Name="chkSun" HorizontalAlignment="Left" Margin="678,247,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="chkMon" HorizontalAlignment="Left" Margin="714,247,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="chkWed" HorizontalAlignment="Left" Margin="791,247,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="chkTue" HorizontalAlignment="Left" Margin="752,248,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="chkThu" HorizontalAlignment="Left" Margin="826,247,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="chkFri" HorizontalAlignment="Left" Margin="865,247,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="chkSat" HorizontalAlignment="Left" Margin="897,247,0,0" VerticalAlignment="Top"/>`

我有一个日期选择器和7个复选框,如(星期日,星期一,…星期六),如果我正在检查任何复选框(例如:太阳,星期一),那么那一天/天(太阳,星期一)必须在日期选择器控件中禁用所有月份。请告诉我怎么做。

谢谢。

在没有最小和最大日期范围的所有月份和年份中禁用Wpf日期选择器中的特定日期

假设您的DatePicker中显示的日期有边界(例如从500天前到未来500天),那么您可以通过CheckBox.CheckedCheckBox.UnChecked事件来处理它。

下面是一个示例代码:

Xaml:

<DatePicker x:Name="dpFrom"  />
<CheckBox  Content="Sunday" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
<CheckBox  Content="Monday"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
<CheckBox  Content="Tuesday"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
<CheckBox  Content="Wednesday" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
<CheckBox  Content="Thursday" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
<CheckBox  Content="Friday"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
<CheckBox  Content="Saturday" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>

代码:

    public MainWindow()
    {
        InitializeComponent();
        dpFrom.DisplayDateStart = DateTime.Now - TimeSpan.FromDays(500); ;
        dpFrom.DisplayDateEnd = DateTime.Now + TimeSpan.FromDays(500);
    }
    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        var chbText = ((CheckBox)sender).Content.ToString();
        DayOfWeek dayOfWeek;
        Enum.TryParse(chbText, out dayOfWeek);
        var minDate = dpFrom.DisplayDateStart ?? DateTime.MinValue;
        var maxDate = dpFrom.DisplayDateEnd ?? DateTime.MaxValue;
        for (var d = minDate; d <= maxDate ; d = d.AddDays(1))
        {
            if (d.DayOfWeek == dayOfWeek)
            {
                dpFrom.BlackoutDates.Add(new CalendarDateRange(d));
            }
        }
    }
    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        var chbText = ((CheckBox)sender).Content.ToString();
        DayOfWeek dayOfWeek;
        Enum.TryParse(chbText, out dayOfWeek);
        var minDate = dpFrom.DisplayDateStart ?? DateTime.MinValue;
        var maxDate = dpFrom.DisplayDateEnd ?? DateTime.MaxValue;
        for (var d = minDate; d <= maxDate ; d = d.AddDays(1))
        {
            if (d.DayOfWeek == dayOfWeek)
            {
                dpFrom.BlackoutDates.Remove(dpFrom.BlackoutDates.First(item => item.Start == d ));
            }
        }
    }

注意我设置每个CheckBoxContentDayOfWeek enum的成员完全相同,以便通过CheckBox.Content添加/删除BlackoutDates