簡體   English   中英

如何設置WPF的DatePicker可選范圍?

[英]How to set DatePicker selectable range for WPF?

我在論壇和msdn上到處搜索,並說無論是XAML還是C#中的SelectedDate,DisplayStartDate,DisplayEndDate都可以使它工作,但是每次嘗試使用其中的任何方法時,我都會丟失指令引用或程序集。 我該如何解決? 我知道我缺少參考,但是使用System或xlmns會怎樣...? 請告訴我如何使它起作用,所以在嘗試使用該屬性時不會出現錯誤。

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#代碼參考:

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;

完整的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>

如果您使用的是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>

如果您希望他們今天只能選擇,那么他們真的什么也不能選擇。 將默認值設置為DateTime.Today並在控件上設置IsEnabled =“ false”。

如果要阻止人們選擇今天以外的任何日期,則可以使用DatePickerBlackoutDates屬性。 像這樣:

myDatePicker.BlackoutDates.Add(new CalendarDateRange(DateTime.Now.AddDays(1), DateTime.MaxValue));

您應該在頁面初始化時執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM