如何为 WPF 设置 DatePicker 可选范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22018690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set DatePicker selectable range for WPF?
提问by TheAmazingKnight
I'm searching everywhere on forums and msdn and it says that SelectedDate, DisplayStartDate, DisplayEndDate, whether in XAML or C# will make it work, but everytime I try to use any of theses, I'm getting missing directive reference or assembly. How do I fix this? I know I'm missing a reference, but what using System or xlmns....? Please tell me how I can make it work, so I don't get errors when I try to use the property.
我在论坛和 msdn 上到处搜索,它说 SelectedDate、DisplayStartDate、DisplayEndDate,无论是在 XAML 还是 C# 中都可以使它工作,但是每次我尝试使用任何这些时,我都会丢失指令引用或程序集。我该如何解决?我知道我缺少一个参考,但是使用 System 或 xlmns 是什么......?请告诉我如何使其工作,以便在我尝试使用该属性时不会出错。
XAML Code & References:
XAML 代码和参考:
x:Class="Data_Query.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<toolkit:DatePicker HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="dateOfBirthPicker" ValueChanged="dateOfBirthPicker_ValueChanged" Margin="0,369,0,0"/>
C# Code References:
C# 代码参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Media; // added to support SolidColorBrush, FontWeights, etc...
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Data_Query.Resources;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Text.RegularExpressions;
Full XAML Code:
完整的 XAML 代码:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="JP APPS" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="Data Query" Margin="0,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--Height of the Grid has to be greater than the ScrollViewer's Height to make it scrollable.-->
<!--Removing the ScrollViewer's Height enables you to see the entire page in a transparent-like view-->
<ScrollViewer Height="605" Width="480" HorizontalAlignment="Left" Margin="0,0,0,-163" VerticalAlignment="Top" Grid.Row="1">
<Grid MinHeight="605" Height="770">
<TextBlock x:Name="firstNameTBL" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="First Name:" VerticalAlignment="Top"/>
<TextBox x:Name="firstNameTB" InputScope="PersonalFullName" KeyUp="TextBox_KeyUp" LostFocus="firstNameTB_LostFocus" HorizontalAlignment="Left" Height="72" Margin="0,42,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="253"/>
<TextBlock x:Name="lastNameTBL" HorizontalAlignment="Left" Margin="10,119,0,0" TextWrapping="Wrap" Text="Last Name:" VerticalAlignment="Top"/>
<TextBox x:Name="lastNameTB" InputScope="PersonalFullName" KeyUp="TextBox_KeyUp" LostFocus="lastNameTB_LostFocus" HorizontalAlignment="Left" Height="72" Margin="0,151,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="253"/>
<TextBlock x:Name="emailAddressTBL" HorizontalAlignment="Left" Margin="10,228,0,0" TextWrapping="Wrap" Text="Email Address:" VerticalAlignment="Top"/>
<TextBox x:Name="emailAddressTB" InputScope="EmailSmtpAddress" KeyUp="TextBox_KeyUp" LostFocus="emailAddressTB_LostFocus" HorizontalAlignment="Left" Height="72" Margin="0,260,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="253"/>
<TextBlock x:Name="dateOfBirthTBL" HorizontalAlignment="Left" Margin="10,337,0,0" TextWrapping="Wrap" Text="Date of Birth:" VerticalAlignment="Top"/>
<toolkit:DatePicker HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="dateOfBirthPicker" ValueChanged="dateOfBirthPicker_ValueChanged" Margin="0,369,0,0"/>
<TextBlock Name="genderTBL" HorizontalAlignment="Left" Margin="10,447,0,0" TextWrapping="Wrap" Text="Gender:" VerticalAlignment="Top"/>
<RadioButton Name="maleRB" GroupName="genderLB" Tap="maleRB_Tap" Content="Male" Checked="maleRB_Checked" HorizontalAlignment="Left" Margin="0,479,0,0" VerticalAlignment="Top"/>
<RadioButton Name="femaleRB" GroupName="genderLB" Tap="femaleRB_Tap" Content="Female" Checked="femaleRB_Checked" HorizontalAlignment="Left" Margin="113,479,0,0" VerticalAlignment="Top"/>
<TextBlock Name="disabilityTBL" HorizontalAlignment="Left" Margin="10,551,0,0" TextWrapping="Wrap" Text="Do you have a disability?" VerticalAlignment="Top"/>
<ToggleButton Name="yesTBU" Content="Yes" Tap="ToggleButton_Tap" Checked="yesTBU_Checked" Height="100" Width="150" Margin="0,583,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ToggleButton Name="noTBU" Content="No" Tap="ToggleButton_Tap" Checked="noTBU_Checked" Height="100" Width="150" Margin="151,583,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Submit" HorizontalAlignment="Stretch" Margin="0,0,250,10" VerticalAlignment="Bottom" Click="submitButton" Height="77"/>
<Button Content="Reset All" HorizontalAlignment="Stretch" Margin="235,0,10,10" VerticalAlignment="Bottom" Click="resetButton" Height="77"/>
</Grid>
</ScrollViewer>
</Grid>
回答by TYY
If you are using WPF you do not need to be using the toolkit namespace which points to the windows phone. In the example project I am working with this is all I have to do to get the DatePicker working.
如果您使用的是 WPF,则不需要使用指向 Windows Phone 的工具包命名空间。在我正在处理的示例项目中,我需要做的就是让 DatePicker 工作。
<Window x:Class="testDatePicker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
</Grid.Resources>
<DatePicker DisplayDateStart="2014/02/28" DisplayDateEnd="2014/02/28" SelectedDate="2014/02/28"></DatePicker>
</Grid>
回答by DLeh
If you want them to only be able to select today, then they really can't select anything. Set the default value to be DateTime.Today and set IsEnabled="false" on the control.
如果你想让他们今天只能选,那他们真的什么都选不了。将默认值设置为 DateTime.Today 并在控件上设置 IsEnabled="false"。
回答by Mashton
If you want to stop people from selecting any date beyond today, then you can use the BlackoutDatesproperty of a DatePicker. Like this:
如果你想从选择超越目前的任何日期停止的人,那么你可以使用BlackoutDates的属性DatePicker。像这样:
myDatePicker.BlackoutDates.Add(new CalendarDateRange(DateTime.Now.AddDays(1), DateTime.MaxValue));
You should do this around the time the page is initialised.
您应该在页面初始化时执行此操作。

