Telerik UI For UWP RadCalendar Control Binding To selected Dates - telerik

In this Telerik doc here:
https://docs.telerik.com/devtools/universal-windows-platform/controls/radcalendar/selection
It says:
Properties
SelectedDateRange (CalendarDateRange?): Gets or sets the first date range in
the current selection or returns null if the selection is empty. Setting this
property in a calendar that supports multiple selections clears existing
selected ranges and sets the selection to the range specified.
SelectedDateRanges (CalendarDateRangeCollection): Holds a collection of all
selection ranges.
I am having trouble binding to the calendar I am mis-understanding the requirements and there is no example of how to do it. I am using A ViewModel approach. My XAML:
<input:RadCalendar
Name="cal"
SelectedDateRange="{x:Bind viewModel.selectedCalendarDateRange, Mode=TwoWay}"
SelectionMode="Multiple"
<input:RadCalendar.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Command="{x:Bind viewModel.calendarSelectCommand}">OK</MenuFlyoutItem>
</MenuFlyout>
</input:RadCalendar.ContextFlyout>
In my ViewModel:
public CalendarDateRange? selectedCalendarDateRange {get=>_calendarDateRange;
set => SetProperty(ref _calendarDateRange,value); }
I am selecting some dates and on mouse up I I get this error:
System.ArgumentOutOfRangeException: The added or subtracted value results in
an un-representable DateTime.
Parameter name: value
at System.DateTime.AddTicks(Int64 value)
at Telerik.UI.Xaml.Controls.Input.CalendarDateRange.
IntersectsWithRange(CalendarDateRange otherDateRange)
at Telerik.UI.Xaml.Controls.Input.
CalendarDateRangeCollection.MergeCollidingRanges
(CalendarDateRange newDataRange, Int32 currentIndex)
at Telerik.UI.Xaml.Controls.Input.
CalendarDateRangeCollection.AddDateRange(CalendarD
How do I correctly set up the binding on this control?

I can see that the type of the selectedCalendarDateRange property of your ViewModel does not match the type of the SelectedDateRange property of the RadCalendar. Note that there is a question mark after the type of the SelectedDateRange. This makes it nullable type. You can try adding the same question mark after the type of the property in your ViewModel, something like this:
public CalendarDateRange? selectedCalendarDateRange {get=>_calendarDateRange; set => SetProperty(ref _calendarDateRange,value); }
The field _calendarDateRange should also be nullable.

Related

Why is Xamarin DatePicker Binding causing a navigation failure?

I am using Prism and Autofac with Xamarin.Forms 4.0 with an MVVM architecture. Using the Navigation.NavigateAsync("MyPage") works unless I have a binding to the Date object with my ViewModel.
The page renders properly and I am navigated to it if my DatePicker has no binding.
<DatePicker x:Name="ProcessStartDate" Format="D" MinimumDate="01/01/2000" />
However the following will cause me to never navigate to the page.
<DatePicker x:Name="ProcessStartDate" Format="D" MinimumDate="01/01/2000" Date="{Binding SelectedStartDate, Mode=TwoWay}"
The property in the View Model, MyVM, looks like this.
private DateTime selectedStartDate;
public DateTime SelectedStartDate
{
get
{
return selectedStartDate;
}
set
{
SetProperty(ref selectedStartDate, value);
sample.ProcessStartDate = value;
}
}
Navigation with the following code fails with the Binding in XAML above:
INavigationResult status;
try
{
var parameters = new NavigationParameters();
parameters.Add("CurrentSample", SelectedSample);
status = await NavigationService.NavigateAsync("MyPage", parameters); //MyPage is registered with MyVM
}
catch (Exception ex)
{
string mess = ex.Message;
}
My work-around is to add an event handler to the code-behind.
<DatePicker x:Name="ProcessStartDate" Format="D" MinimumDate="01/01/2000" DateSelected="OnStartDateSelected"
So now my code-behind has a handler:
void OnStartDateSelected(object sender, DateChangedEventArgs args)
{
SampleDetailsViewModel vm = BindingContext as SampleDetailsViewModel;
vm.SelectedStartDate = args.NewDate;
}
I have a work-around for this page, But I don't want put code in the code-behind. This breaks the MVVM standard that I've managed to maintain on the other seven pages of the app. Am I Binding improperly with the DatePicker?
When Binding SelectedStartDate, you are not initializing it, making it binding to a null, because you have set the Binding Mode to "TwoWay".
Here you can find the various types of binding modes, quoting:
Causes changes to either the source property or the target property to
automatically update the other. This type of binding is appropriate
for editable forms or other fully-interactive UI scenarios.
a solution would be something like this (if you wanna keep the TwoWay mode, and dont mind starting with an default selected):
private DateTime selectedStartDate = DateTime.Now;
Or
Making the binding mode to "OneWayToSource", this makes updates to the binding source without, and not the target (remember that this way you can't change the selected date from the binding, only the datepicker can).
Updates the source property when the target property changes.
Or
If you wanna keep the TwoWay Mode and not having a default date selected, the way you did with code behind is a nice workaround.

Xamarin update viewmodel with command

currently I don't know how to solve my problem. I am writing an app with xamarin.forms.
I have a view which contains the userprofile of the user. For example there is a entry with the username. The field is binded in the twoway mode to the viewmodel:
<Entry x:Name="givennameSurname" Text="{Binding FullName, Mode=TwoWay}" />
The whole userprofile contains some fields which are validated with a behavior:
<behaviors:TelNumBehavior x:Name="NumValidatorUser" IsValid="{Binding Source={x:Reference root}, Path=BindingContext.UserTelNumBehavior, Mode=TwoWay }"></behaviors:TelNumBehavior>
The isValid property is also binded to the viewmodel.
What I want to achieve? I want, that if one of the behaviors validate the input to false, that the userprofile can not be updated.
So I wanted to create a command on a button. The button has an canExecute method: this method checks the booleans in the viewmodel if they all are true or false. If true, I want to force a refresh of the data of viewmodel from the view. I use that canExecute also to prevent unwanted changes, if the UI is in the state, that some input is wrong:
public string FullName
{
get => profile.GivenName;
set
{
if (CanSave())
{
profile.GivenName = value;
OnPropertyChanged();
}
}
}
What is the problem?
I change an input with a behavior, so that the behavior says the input is wrong. Then I edit the username. Then I change the input of the wrong-behavior to true. Now the input of the username does not refresh in the viewmodel. I want to refresh it with the command, but I don't know how to force a refresh from the view to the viewmodel.
As far as I know you need to write what property you changing in your getter and setter so:
OnPropertyChanged("FullName");

Xamarin Forms: Multiple items for a converter

I have a custom view which shows a "No Results" message when a page has no values.
<common:NoResults
IsVisible="{Binding Details,
Converter={ StaticResource EmptyListIsTrueConverter }}"></common:NoResults>
The problem is when the page is loading, there are no values so it shows and should not.
Is there a way to combine a check for an empty list and another Model property like IsBusy into one Converter ?
In IsVisible set the binding to a property DoneLoadingAndNoValues in the viewmodel with default value false.
Set this property to true when loading is done and Details contains no values.
ViewModel (implements INotifyPropertyChanged with OnPropertyChanged):
private bool doneLoadingAndNoValues = false; // default is false
public bool DoneLoadingAndNoValues
{
get { return doneLoadingAndNoValues; }
set
{
doneLoadingAndNoValues= value;
OnPropertyChanged(nameof(DoneLoadingAndNoValues));
}
}
...
// Done loading and Details contains no values:
DoneLoadingAndNoValues = true;
XAML:
<common:NoResults IsVisible="{Binding DoneLoadingAndNoValues}" />
I can't see a way to easily do this in XAML. Try it in your code behind.
When the page is finished loading, execute a function which checks the how many values there are. If there are no values, show your no values message.

Implementation of OnSearchBarButtonPressed in the ListView in Xamarin Form

Is there a way to search a name on the table(Listview) in Xamarin Form without using any MonoTouch components - for crossplatform?
I have a number of names and populated on listview and what I want is that once user types the name and listview would be sorted and it should bring to matched name. I have added searchbar at the top of the listview.
I do not know how to implement OnSearchBarButtonPressed method. I believe that I should not reinvent the wheel again.
SearchBar searchBar = new SearchBar
{
Placeholder = "Search Employee Name",
};
searchBar.SearchButtonPressed += OnSearchBarButtonPressed;
Padding = new Thickness (10, 20, 10, 10);
Content = new StackLayout () {
Children={searchBar,listView}
};
In relation to your other posting here your ListView ItemsSource is a collection of objects.
The ListView control has no way of knowing about your model and wouldn't be able to any filtering by itself.
There appears to be nothing inbuilt into the ListView that would allow you to specify some Predicate to help with this task anyway.
You should therefore do the filtering in the SearchBar.SearchButtonPressed and then re-assign the ListView ItemsSource to reflect the newly filtered ItemsSource you want to show to the end-user. Your filtering logic could then be anything you wanted as your be able to customize this to even filter over many fields etc.
The SearchBar.SearchButtonPressed is just an event which you assign an EventHandler, like you are already doing. You just need to fill in the filtering logic and update the ListView accordingly.

Windows Phone list picker with empty default value

I have scenario where I need to select country from drop down or list picker. I have found how to make this list, but I need to have a default value in this drop-down and I want the default value to be null.
Can someone suggest the best way how to make something like this.
You should bind the SelectedItem value of the drop-down control to a Property in your ViewModel, then avoid setting the property to other than default value in constructor.
This way it will not show any other item, because your SelectedItem property is null.
EDIT:
If I understand you correctly you could do something like this (I'm using Windows Phone Toolkit at http://phone.codeplex.com/
<toolkit:ListPicker ItemsSource="{Binding MyList}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" />
Then your ViewModel could lokk like this:
public ObservableCollection<string> MyList{get;set;}
public string MySelectedItem{
get{ return _mySelectedItem; }
set{ _mySelectedItem = value;
OnPropertyChanged("MySelectedItem");
}

Resources