Binding a CustomDateControl to Data Model in WPF C#

Mesh Ka 345 Reputation points
2023-12-27T05:44:04.3433333+00:00

Hi,

I created a CustomDateControl to Collect Partial Date from my Users by Collecting Year, Month and Date each on a separate textbox Inside a One TextBox Respectively.

I know that it is not a good idea to Skip DateTime DataType usage for Dates, but My dates are not full, they are mixed; Partial and Full dates, meaning Sometimes my users provide Only the Year and Sometimes the Year and the Month and Sometimes Full. So, in that situation DateTime will not be Compatible!

When the Users clicks inside the Main texbox or it gets focus, it displays the three texboxes to collect partial date and when the focus gets away it Displays the Date fully in the main TextBox as “dd/mm/yyyy”.

 

 customDateControl

 

Now everything is working nicely except that I am getting a tough time on how to bind this custom control to my Data Model so that I can Display and Update the data Correctly etc. In the Database the DataType is NvarChar(10) but when it comes to Model things need to Change if I am not wrong, and I think the whole issue lies here.

Additionally, I want to restrict the users from writing the Day before they write the Month and the Month before the Year.

Here is the repos on github of what I tried (with the DATABASE SCRIPT).

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,947 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2023-12-28T05:12:02.0566667+00:00

    Hi,
    in your approach you can change your Model:

    namespace DateTextBoxApp.Models
    {
    	public class CustomerModel : INotifyPropertyChanged
    	{
    		private int _CustomerID;
    		public int CustomerID
    		{
    			get { return _CustomerID; }
    			set
    			{
    				if (value != _CustomerID)
    				{
    					_CustomerID = value;
    					OnPropertyChanged(nameof(CustomerID));
    				}
    			}
    		}
    
    		private string _FirstName;
    		public string FirstName
    		{
    			get { return _FirstName; }
    			set
    			{
    				if (value != _FirstName)
    				{
    					_FirstName = value;
    					OnPropertyChanged(nameof(FirstName));
    				}
    			}
    		}
    
    		private object _DateOfBirth;
    		public object DateOfBirth
    		{
    			get { return _DateOfBirth; }
    			set
    			{
    				if (value != _DateOfBirth)
    				{
    					_DateOfBirth = new DOB(value);
    					OnPropertyChanged(nameof(DateOfBirth));
    				}
    			}
    		}
    
    
    
    		//*********************************************************************************************************************************************************************************************************************88
    		//*********************************************************************************************************************************************************************************************************************88
    		//*********************************************************************************************************************************************************************************************************************88
    
    		public event PropertyChangedEventHandler PropertyChanged;
    		private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    		{
    			PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    		}
    		public class DOB
    		{
    			public DOB(object value)
    			{
    				var dateParts = value.ToString().Split('/');
    				Day = dateParts.Length > 0 ? dateParts[0] : null;
    				Month = dateParts.Length > 1 ? dateParts[1] : null;
    				Year = dateParts.Length > 2 ? dateParts[2] : null;
    			}
    			public string FullDate { get => this.ToString(); }
    			public string Day { get; set; }
    			public string Month { get; set; }
    			public string Year { get; set; }
    
    			public override string ToString()
    			{
    				return $"{Day}/{Month}/{Year}";
    			}
    		}
    	}
    }
    
    

    Result:

    x


  2. Peter Fleischer (former MVP) 19,326 Reputation points
    2023-12-28T19:57:52.46+00:00

    Hi,
    in your code you do:

    1. Set DataContext of MainWindow to instace of MainWindowViewModel (in ctor of MainWindow)
    2. in MainWindowViewModel you have a SelectedCostumer property
    3. SelectedCostumer property is set to CustomerModel instance from CustomerData
    4. in XAML of MainWindow you use ContentControl
    5. Content of ContentControl is binded to DateOfBirth property of SelectedCostumer
    6. Style of ContentControl is set to StaticResource DateTextBox
    7. Template of StyleDateTextBox is set to StaticResource DateTextBoxTemplate
    8. in TextBoxes (in DateTextBoxTemplate) Text properties are binded to property Day, Month, ... in DataContext (= DateOfBirth)
    9. in your code the type of DateOfBirth is string. Type string have not properties Day, Month, ...
    10. type of SelectedCostumer is CustomerModel with property DateOfBirth (type string)

    In my solution:

    1. I change the type of DateOfBirth property to object
    2. in this case property DateOfBirth can be used for string (data from database) and for sub object DOB for binding Day, Month, ... and with ToString method for saving in database
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.