Home c# How to change the default date in DatePicker wpf

How to change the default date in DatePicker wpf




please tell me how to remove the default DatePicker wpf date which is: 01.01.0001 and set your own date by default, let’s say 01.01.2000. Thank you.

 enter image description here

Answer 1, authority 100%

Suppose we have a standard class of our object:

public class ItemViewModel
  public string Name {get; set; }
  public DateTime Date {get; set; }

And there is a bound collection in the main ViewModel:

public ObservableCollection & lt; ItemViewModel & gt; Items {get; set; }

In standard DataGrid markup with autogeneration:

& lt; DataGrid ItemsSource = "{Binding Items}" / & gt;

Now, if you have the same option, then it is enough to rewrite the property with the date by adding a default value to it:

public DateTime Date {get; set; } = new DateTime (2020,1,1);

Another option is to set the default via XAML.
To do this, bindings have properties FallbackValue and TargetNullValue .

Let’s make some simple markup:

& lt; DataGrid ItemsSource = "{Binding Items}" AutoGenerateColumns = "False" & gt;
  & lt; DataGrid.Columns & gt;
    & lt; DataGridTextColumn Header = "Name" Binding = "{Binding Name}" / & gt;
    & lt; DataGridTextColumn Header = "Date" Binding = "{Binding Data, FallbackValue = '2019.01.02'}" / & gt;
  & lt; /DataGrid.Columns>
& lt; / DataGrid & gt;

The result will be something like this:


Answer 2


& lt; my: DatePicker SelectedDate = "{x: Static sys: DateTime.Now}" / & gt; // change to your date
// add a link
xmlns: sys = "clr-namespace: System; assembly = mscorlib"
// or so
public MainWindow ()
  InitializeComponent ();
  dpDate.SelectedDate = DateTime.Today;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions