【问题标题】:How can I add minimum date validation check for DatePicker in Xamarin如何在 Xamarin 中为 DatePicker 添加最小日期验证检查
【发布时间】:2017-12-20 16:04:23
【问题描述】:

我有两个DatePicker(命名为fromdate 和todate)和一个按钮(命名为save)。

默认情况下,今天的日期将填充到DatePicker 中。我添加了最小日期验证(用户只能选择今天或更大的日期。

如果今天的日期是 5 月 30 日,并且用户从 fromdate 中选择了 6 月 15 日,然后单击确定。现在默认的今天日期显示在todate 中。 我想要一个验证,比如如果用户在fromdate 中选择 6 月 15 日并单击确定,那么todate 应该填充有fromdate

【问题讨论】:

    标签: c# user-interface xamarin datepicker xamarin.forms


    【解决方案1】:

    这可以通过将 toDate DatePicker 的 MinimumDate 绑定到 FromDate 属性来完成。

    我不知道你的代码,所以我举个例子:

    假设您有这样的 XAML:

    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage 
        xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        xmlns:local="clr-YourProject" 
        x:Class="YourProject.YourClass">
    
        <StackLayout>
            <DatePicker Date="{Binding FromDate}" MinimumDate="{Binding FromMiminumDate}" />
    
            <DatePicker Date="{Binding ToDate}" MinimumDate="{Binding FromDate}" />
        </StackLayout>
    </ContentPage>
    

    你有一个像这样的 ViewModel:

    public class YourViewModel : INotifyPropertyChanged
    {
        public YourViewModel ()
        {
            FromMiminumDate = DateTime.Today;
        }
    
        private DateTime _fromDate;
        public DateTime FromDate
        {
            get { return _fromDate; }
            set
            {
                if (_fromDate == value)
                    return;
    
                _fromDate = value;
                NotifyPropertyChanged (nameof(FromDate));
            }
        }
    
        private DateTime _toDate;
        public DateTime ToDate
        {
            get { return _toDate; }
            set
            {
                if (_toDate == value)
                    return;
    
                _toDate = value;
                NotifyPropertyChanged (nameof(ToDate));
            }
        }
    
        private DateTime _fromMiminumDate;
        public DateTime FromMiminumDate
        {
            get { return _fromMiminumDate; }
            set
            {
                if (_fromMiminumDate == value)
                    return;
    
                _fromMiminumDate = value;
                NotifyPropertyChanged (nameof(FromMiminumDate));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        void NotifyPropertyChanged (string propertyName)
        {
            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
        }
    }
    

    这将使您每次更改在 FromDate Picker 中选择的日期时,ToDate Picker 的 MinimumDate 也会更改。

    当然,后面的 XAML 代码也很简单:

    public YourClass ()
    {
        var VM = new YourViewModel ();
    
        InitializeComponent ();
    
        BindingContext = VM;
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      所以你有两个 DatePickers:fromDate 和 toDate。

      您可以在 fromDate 中添加一个名为“PropertyChanged”的属性:

      <DatePicker
      x:Name="fromDate"
      PropertyChanged="FromDate_Changed"
      />
      
      <DatePicker
      x:Name="toDate"
      />
      

      现在您可以进入您的 xaml.cs 文件并将以下内容添加到构造函数中:

      public ItemsPage()
      {
          InitializeComponent();
          fromDate.MinimumDate = DateTime.Now;
          toDate.MinimumDate = DateTime.Now;
      }
      

      现在使用两个 DatePickers,您必须选择一个不是过去的日期。

      然后你可以创建方法“FromDate_Changed”(如果它没有被自动创建):

      private void FromDate_Changed(object sender, PropertyChangedEventArgs e)
      {
          toDate.MinimumDate = fromDate.Date;
      }
      

      现在,每当您从 fromDate 选择器中更改日期时,toDate 中的最短日期将自动更改为您在 fromDate 中选择的日期。

      我知道这个问题已经有 2 年历史了,但也许将来如果有人正在寻找那个问题,他会找到一个更简单的解决方案。

      请原谅我的英语。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2012-11-19
        • 1970-01-01
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多