CalendarDatePicker绑定空值

时间:2016-09-29 16:27:50

标签: c# asp.net xaml uwp model-binding

我正在使用CalendarDatePicker

<CalendarDatePicker x:Name="DatePick" DisplayMode="Decade" Date="{Binding DisplayValue, Mode=TwoWay}"/>

问题是,当我绑定Date属性时,Picker不再显示默认的占位符文本(选择日期),而是如果属性为null(属性始终设置为null),它现在显示01.01.1916。

public DateTimeOffset? DisplayValue {get;set;}

如果我手动将null作为Date的值,它现在再次显示PlaceholderText。

DatePick.Date=null;

绑定仍然有效/我在选择日期时会获得所需的值。 那么是否有可能在不使用代码隐藏的情况下显示占位符文本?

4 个答案:

答案 0 :(得分:3)

似乎是calendardatepicker&#34; min value&#34;总是当前年减少100,我已经修改了我的代码:

# Title

<img align="left" src="./documentation/images/A.jpg" alt="Made with Angular" title="Angular" hspace="20"/>
<img align="left" src="./documentation/images/B.png" alt="Made with Bootstrap" title="Bootstrap" hspace="20"/>
<img align="left" src="./documentation/images/C.png" alt="Developed using Browsersync" title="Browsersync" hspace="20"/>
<br/><br/><br/><br/><br/>

## Table of Contents...

首选解决方案

如果使用x:Bind而不是Binding,则所有工作都按预期工作,如果绑定日期值为null,则可以看到占位符。

直接在XAML页面中:

  public class CalendarDatePickerEx : CalendarDatePicker
  {
    public DateTimeOffset? DefaultValue { get; set; }

    public static readonly DependencyProperty DefaultValueProperty =
      DependencyProperty.Register("DefaultValue", typeof(DateTimeOffset), typeof(CalendarDatePickerEx), new PropertyMetadata(null, (sender, e) =>
      {
        if ((DateTimeOffset?)e.NewValue != null && ((CalendarDatePickerEx)sender).Date.Value.Date.Year == DateTime.Today.Year - 100)
        {
          ((CalendarDatePickerEx)sender).SetDisplayDate((DateTimeOffset)e.NewValue);
          ((CalendarDatePickerEx)sender).Date = null;
        }
        else if ((DateTimeOffset?)e.NewValue == null && ((CalendarDatePickerEx)sender).Date.Value.Date.Year == DateTime.Today.Year - 100)
        {
          {
            ((CalendarDatePickerEx)sender).SetDisplayDate(DateTimeOffset.Now);
            ((CalendarDatePickerEx)sender).Date = null;
          }
        }
      }));
  }

或在DataTemplate中:

<CalendarDatePicker Date="{x:Bind DataContext.DisplayValue, Mode=TwoWay}"/>

答案 1 :(得分:1)

我找到了一个有效的解决方案。 因此我扩展了默认的calendarDatePicker。 我添加了一个额外的可绑定属性,它既可以作为defaultValue Setter用于选择日期,也可以作为Date值在需要时设置为。

public class CustomNewCalendarDatePicker : CalendarDatePicker
{
public DateTimeOffset? DefaultValue { get; set; }

public static readonly DependencyProperty DefaultValueProperty =
  DependencyProperty.Register("DefaultValue", typeof(DateTimeOffset), typeof(CustomNewCalendarDatePicker), new PropertyMetadata(null, (sender, e) =>
  {
    if ((DateTimeOffset?)e.NewValue != null && ((CustomNewCalendarDatePicker)sender).Date.Value.Date.Year == 1916)
    {
      ((CustomNewCalendarDatePicker)sender).SetDisplayDate((DateTimeOffset)e.NewValue);
      ((CustomNewCalendarDatePicker)sender).Date = null;
    }
    else if ((DateTimeOffset?)e.NewValue == null && ((CustomNewCalendarDatePicker)sender).Date.Value.Date.Year == 1916)
    {
      {
        ((CustomNewCalendarDatePicker)sender).SetDisplayDate(DateTimeOffset.Now);
        ((CustomNewCalendarDatePicker)sender).Date = null;
      }
    }
  }));
}

答案 2 :(得分:1)

我的解决方案,对我来说很好用

public class CalendarDatePickerExt : CalendarDatePicker
{
    public CalendarDatePickerExt()
    {
        this.DateChanged += CalendarDatePickerExt_DateChanged;
    }

    private void CalendarDatePickerExt_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
    {
        if (args.NewDate != null && args.NewDate.Value.Year == DateTime.Today.Year - 100)
        {
            this.SetDisplayDate(DateTimeOffset.Now);
            this.Date = null;
        }
    }
}

答案 3 :(得分:0)

在具有该属性的类中,您需要实现接口INotifyPropertyChanged。在属性DisplayValue中,您必须调用onPropertyChanged方法。

示例代码:

的Xaml:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <CalendarDatePicker x:Name="DatePick" DisplayMode="Decade" Date="{Binding DisplayValue, Mode=TwoWay}"/>
</Grid>

MainPage.cs:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        ViewModel viewModel = new ViewModel();
        DataContext = viewModel;
    }
}

自定义类(ViewModel):

class ViewModel : INotifyPropertyChanged
    {

        private DateTimeOffset _displayValue;

        public DateTimeOffset? DisplayValue
        {
            get
            {
                return _displayValue;
            }
            set
            {
                if (value != null) _displayValue = value.Value;
                OnPropertyChanged();
            }
        }

        public ViewModel()
        {
            DisplayValue = new DateTimeOffset(2016, 9, 29, 6, 39, 10, 3, new TimeSpan());
        }




        public event PropertyChangedEventHandler PropertyChanged;


        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }