0
votes

I've got a View with a DatePicker bound to a DateTime property in my ViewModel. However, the date value always appears as null, even when I select a different value than the default. Anyone have success with the DatePicker? If so, what am I getting wrong?

Relevant Code:

View:

<DatePicker x:Name="startDateUpdatePick" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="120" Margin="1.5" 
   Text="{Binding SelectedProjectData.ProjectStartDate, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}"/>

ViewModel:

public PSViewModel()
    {
        SelectedProjectData = new Project();
    }

Model:

public class Project : INotifyPropertyChanged
{
    private DateTime _projectStartDate;

    public DateTime ProjectStartDate
    {
        get { return _projectStartDate; }
        set
        {
            _projectStartDate = value;
            RaisePropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged([CallerMemberName] string caller = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
    }
}

Edit: the XAML is bound to a non-nullable DateTime, but I have other datepickers in the same setup that are bound to nullable DateTime and they don't work either.

2
What's the DataContext of the DatePicker? And your ViewModel won't even compile, so who knows what's actually going on in there. Project looks correct. - user1228
Also, grab you a copy of Snoop. You can investigate binding errors easily at runtime. - user1228
is SelectedProjectData a public property? Is your view's DataContext set to an instance of PSViewModel? - plast1k

2 Answers

4
votes

The property is SelectedDate, not Text.

When I was a starveling lad, growing up homeless and feral in the lawless, filthy, labyrinthine back alleys of Palo Alto, I once met an ancient, dying one-eyed beggar. He held my thin little arm in an iron, clawlike grip, drew me close, and imparted to me the root of all earthly wisdom with his final, rasping breath:

Read... the... documentation...

He looked exactly like Marty Feldman.

0
votes

View:

<StackPanel Orientation="Horizontal">
    <DatePicker SelectedDate="{Binding SelectedDate, 
        UpdateSourceTrigger=PropertyChanged}"
        DisplayDateStart="{Binding StartDate}" DisplayDateEnd="{Binding EndDate}"/>
    <Label Content="{Binding SelectedDate}"/>
</StackPanel>

ViewModel

   public class MainWindowViewModel : INotifyPropertyChanged
    {
        private string _selectedDate;
        public string SelectedDate
        {
            get
            {
                return _selectedDate;
            }
            set
            {
                _selectedDate = value;
                RaisePropertyChanged();
            }
        }

        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

        public MainWindowViewModel()
        {
            StartDate = new DateTime(2020, 1, 1);
            EndDate = new DateTime(2020, 3, 31);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged([CallerMemberName] string property = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
        }
    }
}