Windows商店应用日期选择器null日期

时间:2013-12-12 05:17:18

标签: c# windows-runtime microsoft-metro windows-store-apps winrt-xaml

我需要在windows store app,windows 8中使用datepicker,但我无法找到任何控件,所以我使用了这个:

jasonmitchell / WinRT-XAML-DatePicker

https://github.com/jasonmitchell/WinRT-XAML-DatePicker/blob/master/Samples/WinRTDatePicker.Samples.sln

但我的问题是,默认情况下它显示今天的日期,我不想要,我不想显示任何日期,如果用户没有申请任何日期

如果有人想在windows metro应用程序中使用日期选择器,这将是很棒的,

我知道Windows 8.1具有该控件但我现在无法移动到Windows 8.1!

代码示例:

  <WinRTDatePicker:DatePicker IsEnabled="True" x:Name="dtActualFinishDate" Width="370"/>

    i want to set value if 

    if (Booking.ActualToDteTme.HasValue)
                {
                    dtActualFinishDate.SelectedDate = Booking.ActualToDteTme.Value;
    }

所以如果ActualToDteTme有任何值,我想要另外申请,我需要保持datepicker为空白,

现在它显示为当前日期,当我点击更新时,它更新了当前日期!!那是不对的行为!!

由于

2 个答案:

答案 0 :(得分:1)

我正在为您提供最新的DatePicker&amp; SelectedDateChangedEventArgs课程。从Github使用这些类。我添加了默认的“月”,“日期”,“年”占位符文字。默认值为null&amp;如果有人选择占位符文本,那么它也将为空。

using System;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace WinRTDatePicker
{
    [TemplatePart(Name = "_DayOptions", Type = typeof(ComboBox))]
    [TemplatePart(Name = "_MonthOptions", Type = typeof(ComboBox))]
    [TemplatePart(Name = "_YearOptions", Type = typeof(ComboBox))]
    public sealed class DatePicker : Control
    {
        public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(DatePicker), new PropertyMetadata(default(DateTime?), SelectedDateChangedCallback));
        public static readonly DependencyProperty DayOptionFormatProperty = DependencyProperty.Register("DayOptionFormat", typeof(string), typeof(DatePicker), new PropertyMetadata(default(string)));
        public static readonly DependencyProperty MonthOptionFormatProperty = DependencyProperty.Register("MonthOptionFormat", typeof(string), typeof(DatePicker), new PropertyMetadata(default(string)));

        public event EventHandler<SelectedDateChangedEventArgs> SelectedDateChanged; 

        private readonly ObservableCollection<string> daysInRange = new ObservableCollection<string>();
        private readonly ObservableCollection<string> monthsInRange = new ObservableCollection<string>();
        private readonly ObservableCollection<string> yearsInRange = new ObservableCollection<string>();

        public DatePicker()
        {
            DefaultStyleKey = typeof(DatePicker);

            //SelectedDate = DateTime.Today;
            DayOptionFormat = "dd dddd";
            MonthOptionFormat = "MMMM";
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            monthsInRange.Clear();
            monthsInRange.Add("Month");
            for (int i = 1; i <= 12; i++)
            {
                DateTime monthStart = new DateTime(DateTime.Now.Year, i, 1);
                monthsInRange.Add(monthStart.ToString(MonthOptionFormat));
            }

            CreateBindings();
            SetSelectedDate(SelectedDate);

            DayOptions.SelectionChanged += DayOptionsOnSelectionChanged;
            MonthOptions.SelectionChanged += MonthOptionsOnSelectionChanged;
            YearOptions.SelectionChanged += YearOptionsOnSelectionChanged;
        }

        private void SetSelectedDate(DateTime? newSelectedDate)
        {
            if (DayOptions != null && MonthOptions != null && YearOptions != null)
            {
                var TempNewSelectedDate = newSelectedDate;
                if (newSelectedDate == null)
                {
                    newSelectedDate = DateTime.Today;
                }
                daysInRange.Clear();
                yearsInRange.Clear();

                daysInRange.Add("Date");
                for (int i = 1; i <= DateTime.DaysInMonth(newSelectedDate.Value.Year, newSelectedDate.Value.Month); i++)
                {
                    DateTime date = new DateTime(newSelectedDate.Value.Year, newSelectedDate.Value.Month, i);
                    daysInRange.Add(date.ToString(DayOptionFormat));
                }

                int minYear = newSelectedDate.Value.Year - 10;
                int maxYear = newSelectedDate.Value.Year + 10;

                yearsInRange.Add("Year");
                for (int i = minYear; i <= maxYear; i++)
                {
                    yearsInRange.Add(i.ToString());
                }

                if (TempNewSelectedDate != null)
                {
                    DayOptions.SelectedIndex = newSelectedDate.Value.Day;
                    MonthOptions.SelectedIndex = newSelectedDate.Value.Month;
                    YearOptions.SelectedItem = newSelectedDate.Value.Year.ToString();
                }

                else
                {
                    DayOptions.SelectedIndex = 0;
                    MonthOptions.SelectedIndex = 0;
                    YearOptions.SelectedIndex = 0;
                }
            }
        }

        private void CreateBindings()
        {
            Binding dayOptionsBinding = new Binding { Source = daysInRange, Mode = BindingMode.OneWay };
            DayOptions.SetBinding(ItemsControl.ItemsSourceProperty, dayOptionsBinding);

            Binding monthOptionsBinding = new Binding { Source = monthsInRange, Mode = BindingMode.OneWay };
            MonthOptions.SetBinding(ItemsControl.ItemsSourceProperty, monthOptionsBinding);

            Binding yearOptionsBinding = new Binding { Source = yearsInRange, Mode = BindingMode.OneWay };
            YearOptions.SetBinding(ItemsControl.ItemsSourceProperty, yearOptionsBinding);
        }

        private void UpdateSelectedDateFromInputs()
        {
            if (YearOptions.SelectedIndex > 0 && MonthOptions.SelectedIndex > 0 && DayOptions.SelectedIndex > 0)
            {
                int year = int.Parse(YearOptions.SelectedValue.ToString());
                int month = MonthOptions.SelectedIndex;
                int day = DayOptions.SelectedIndex;

                int maxDaysInMonth = DateTime.DaysInMonth(year, month);
                if (day > maxDaysInMonth)
                {
                    day = maxDaysInMonth;
                    DayOptions.SelectedIndex = maxDaysInMonth - 1;
                }

                if (month == 0)
                    month = 1;

                if (day == 0)
                    day = 1;

                SelectedDate = new DateTime(year, month, day);
            }
            else if (YearOptions.SelectedIndex == 0 && MonthOptions.SelectedIndex == 0 && DayOptions.SelectedIndex == 0)
            {
                SelectedDate = null;
            }
        }

        private void UpdateDayOptions()
        {
            if (SelectedDate != null)
            {
                int selectedDayIndex = DayOptions.SelectedIndex;
                int month = MonthOptions.SelectedIndex;

                if (month != 0)
                {
                    daysInRange.Clear();
                    daysInRange.Add("Date");
                    for (int i = 1; i <= DateTime.DaysInMonth(SelectedDate.Value.Year, month); i++)
                    {
                        DateTime date = new DateTime(SelectedDate.Value.Year, month, i);
                        daysInRange.Add(date.ToString(DayOptionFormat));
                    }

                    DayOptions.SelectedIndex = selectedDayIndex;  
                }
            }
        }

        private void DayOptionsOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            UpdateSelectedDateFromInputs();
        }

        private void MonthOptionsOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            UpdateSelectedDateFromInputs();
            UpdateDayOptions();
        }

        private void YearOptionsOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            UpdateSelectedDateFromInputs();
            UpdateDayOptions();
        }

        private static void SelectedDateChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            DateTime oldValue = args.OldValue != null ? (DateTime)args.OldValue : DateTime.Today.AddDays(-1);
            DateTime? newValue = (DateTime?)args.NewValue;

            if (newValue != oldValue)
            {
                DatePicker datePicker = (DatePicker)obj;
                datePicker.SetSelectedDate(newValue);

                if (datePicker.SelectedDateChanged != null)
                    datePicker.SelectedDateChanged(datePicker, new SelectedDateChangedEventArgs(newValue));
            }
        }

        public DateTime? SelectedDate
        {
            get { return (DateTime?)GetValue(SelectedDateProperty); }
            set { SetValue(SelectedDateProperty, value); }
        }

        public string DayOptionFormat
        {
            get { return (string)GetValue(DayOptionFormatProperty); }
            set { SetValue(DayOptionFormatProperty, value); }
        }

        public string MonthOptionFormat
        {
            get { return (string)GetValue(MonthOptionFormatProperty); }
            set { SetValue(MonthOptionFormatProperty, value); }
        }

        private ComboBox DayOptions
        {
            get { return (ComboBox)GetTemplateChild("_DayOptions"); }
        }

        private ComboBox MonthOptions
        {
            get { return (ComboBox)GetTemplateChild("_MonthOptions"); }
        }

        private ComboBox YearOptions
        {
            get { return (ComboBox)GetTemplateChild("_YearOptions"); }
        }
    }
}

using System;

namespace WinRTDatePicker
{
    public class SelectedDateChangedEventArgs : EventArgs
    {
        private readonly DateTime? newDate;

        public SelectedDateChangedEventArgs(DateTime? newDate)
        {
            this.newDate = newDate;
        }

        public DateTime? NewDate
        {
            get { return newDate; }
        }
    }
}

答案 1 :(得分:0)

要使绑定生效,您应将SelectedDateProperty更改为

public static readonly DependencyProperty SelectedDateProperty = 
    DependencyProperty.Register("SelectedDate",
                                typeof(object),
                                typeof(NullableDatePicker),
                                new PropertyMetadata(null, SelectedDateChangedCallback));

避免错误:

  

错误:转换器无法转换类型&#39; System.DateTime的值,   mscorlib,版本= 4.0.0.0,文化=中性,   公钥= b77a5c561934e089&#39;输入   &#39; Nullable`1&#39 ;;