{"id":2569,"date":"2022-08-30T15:25:57","date_gmt":"2022-08-30T15:25:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/03\/wpf-timepicker-binding-to-set-date-and-time-at-runtime-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:57","modified_gmt":"2022-08-30T15:25:57","slug":"wpf-timepicker-binding-to-set-date-and-time-at-runtime-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/wpf-timepicker-binding-to-set-date-and-time-at-runtime-collection-of-common-programming-errors\/","title":{"rendered":"WPF- TimePicker Binding to set date and time at runtime-Collection of common programming errors"},"content":{"rendered":"<p>In my Wpf app, I&#8217;ve two TimePickers. I&#8217;ve used binding for them but their time is not updating. I also want to set selected date for these TimePickers.I tried to bind it as below. But, it won&#8217;t work. Actual problem here is TimePickers are inside DataTemplate.<\/p>\n<p>Here is xaml:<\/p>\n<pre><code>\n  \n    \n    \n  \n\n<\/code><\/pre>\n<p>Binding with properits in C#:<\/p>\n<pre><code>    private DateTime _dateTime1;\n    public DateTime StartValue\n    {\n        get\n        {\n            return _dateTime1;\n        }\n        set\n        {\n            _dateTime1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);\n            OnPropertyChanged(\"StartValue\");\n        }\n    }\n\n    private DateTime _dateTime2;\n    public DateTime EndValue\n    {\n        get\n        {\n            return _dateTime2;\n        }\n        set\n        {\n            _dateTime2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);\n            OnPropertyChanged(\"EndValue\");\n        }\n    }\n\n    protected virtual void OnPropertyChanged(String time)\n    {\n        if (System.String.IsNullOrEmpty(time))\n        {\n            return;\n        }\n        if (PropertyChanged != null)\n        {\n            PropertyChanged(this, new PropertyChangedEventArgs(time));\n        }\n    }\n\n    #region INotifyPropertyChanged Members\n    public event PropertyChangedEventHandler PropertyChanged;\n    #endregion\n<\/code><\/pre>\n<p>Please suggest some ideas?<\/p>\n<ol>\n<li>\n<p>You are trying to set your values in your property setters which will never get called so the property value will never be set (and therefore never notified, and the TimePicker will never update).<\/p>\n<p>Why not try setting the properties in the constructor of your ViewModel. I&#8217;m not sure what your ViewModel is called but here goes:<\/p>\n<pre><code>public class MyViewModel : INotifyPropertyChanged\n{\n\npublic MyViewModel()\n{\n    StartValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);\n    EndValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);\n}\n\nprivate DateTime _dateTime1;\npublic DateTime StartValue\n{\n    get\n    {\n        return _dateTime1;\n    }\n    set\n    {\n        if (_dateTime1.Equals(value)) return;\n        _dateTime1 = value; \n        OnPropertyChanged(\"StartValue\");\n    }\n}\n\nprivate DateTime _dateTime2;\npublic DateTime EndValue\n{\n    get\n    {\n        return _dateTime2;\n    }\n    set\n    {\n        if (_dateTime2.Equals(value)) return;\n        _dateTime2 = value;\n        OnPropertyChanged(\"EndValue\");\n    }\n}\n\nprotected virtual void OnPropertyChanged(String time)\n{\n    if (System.String.IsNullOrEmpty(time))\n    {\n        return;\n    }\n    if (PropertyChanged != null)\n    {\n        PropertyChanged(this, new PropertyChangedEventArgs(time));\n    }\n}\n\n#region INotifyPropertyChanged Members\npublic event PropertyChangedEventHandler PropertyChanged;\n#endregion\n}\n<\/code><\/pre>\n<p>This will ensure the PropertyChanged events are fired for both properties and as long as your binding is correct you will see the values update.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-03 02:34:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In my Wpf app, I&#8217;ve two TimePickers. I&#8217;ve used binding for them but their time is not updating. I also want to set selected date for these TimePickers.I tried to bind it as below. But, it won&#8217;t work. Actual problem here is TimePickers are inside DataTemplate. Here is xaml: Binding with properits in C#: private [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2569","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2569"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2569\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}