【问题标题】:How to clear DatePicker through binding with SelectedDate如何通过与 SelectedDate 绑定来清除 DatePicker
【发布时间】:2012-01-31 15:58:36
【问题描述】:

我有一个具有以下绑定的日期选择器。

<Controls:DatePicker SelectedDate="{Binding Person.BirthDate, Mode=TwoWay}" />
<Button Content="Clear" Command="{Binding ClearDateOperation}" />

这是“人”类:

public class Person
{
    public DateTime? BirthDate { get; set; }
}

BirthDate 的初始值为空。 我想解决的问题可以这样重现:

  1. 用户在 datePicker 中输入了“cvgdfgfg”
  2. 单击“清除”按钮。
  3. 在此之后不会清除 datePicker。

这是 ClearDateOperation 的实现

ClearDateCommand = new DelegateCommand(ClearDate);

private void ClearDate()
        {
            Person.BirthDate = null;
        }

有什么想法吗?提前致谢。

ps。我使用 Silverlight 4、.NET 4、VS 2010 附言。您可以使用此链接下载一个包含该问题的简单示例。 Silverlight app

【问题讨论】:

    标签: .net wpf silverlight


    【解决方案1】:

    看起来你需要实现 INotifyPropertyChanged:

    public class Person : INotifyPropertyChanged
    {
        private DateTime? m_BirthDate;
    
        public DateTime? BirthDate
        { 
         get { return m_BirthDate;}
         set
          { 
             m_BirthDate = value;
             this.OnPropertyChanged("BirthDate");
          }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    【讨论】:

    • 感谢您的回复。我做到了。但问题并没有消失。
    • 您可以尝试更改日期选择器的 xaml 以绑定到文本而不是选定日期吗?
    【解决方案2】:
    Person.BirthDate = DateTime.MinDate
    

    或者类似的东西

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多