Reputation: 187
I am working on converting a project from a standard Xamarin.Forms mobile application to use the MVVM format. Prior to converting to MVVM, all of the app's functionality worked as expected. After setting up my ViewModel for one of my Views and adding the SelectedDate
property to my DatePicker
, I started receiving an error: The property 'SelectedDate' was not found in type 'DatePicker'.
After starting a clean all and rebuild all, I also began receiving another error: No property, BindableProperty, or event found for "SelectedDate", or mismatching type between value and property.
The applicable portion of my XAML file:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="StarTracker.AddAdventurePage"
BackgroundColor="{StaticResource lightBlue}">
<StackLayout HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand">
<CollectionView x:Name="NewAdventure"
HorizontalOptions="CenterAndExpand"
SelectionMode="None">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Text="Date Played: "
FontFamily="Axion"
TextColor="{StaticResource red}"
Grid.Row="0"
Grid.Column="0"/>
<DatePicker x:Name="PlayedDatePicker"
FontFamily="Axion"
Grid.Row="0"
Grid.Column="1"
SelectedDate="{Binding Date}"
TextColor="{StaticResource darkBlue}"
MinimumDate="08/17/2017"
MaximumDate="{x:Static sys:DateTime.Today}">
<DatePicker.Format>MM/dd/yyyy</DatePicker.Format>
</DatePicker>
ViewModel:
private DateTime date;
public DateTime Date
{
get { return date; }
set
{
date = value;
OnPropertyChanged("Date");
}
}
After finding How to binding property in a behavior?, I changed my ViewModel to derive from BindableObject
in addition to INotifyPropertyChanged
, then changed the Date getter/setter to:
public static readonly BindableProperty DateProperty = BindableProperty.Create(
nameof(Date),
typeof(DateTime),
typeof(DatePicker));
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
set
{
SetValue(DateProperty, value);
OnPropertyChanged("Date");
}
}
I still have IntelliSense telling me that SelectedDate
is not a property in DatePicker
, which is not accurate according to https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datepicker?redirectedfrom=MSDN&view=netframework-4.8#properties. The SelectedItem
bindings in my other Pickers
work without issues.
What am I missing that will allow me to use the SelectedDate
property in my DatePicker
and have the information saved via my DataBindings?
Upvotes: 0
Views: 446
Reputation: 89102
Xamarin Forms DatePicker does not have a SelectedDate
property, it has a Date property
Date of type DateTime, the selected date, which defaults to the value DateTime.Today.
the docs you linked to in your post are for the System.Windows.Controls.DatePicker
Upvotes: 1