Windows Phone list picker with empty default value - windows-phone-7

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");
}

Related

Telerik UI For UWP RadCalendar Control Binding To selected Dates

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.

How to bind a view model property to a XAML control

Please let me know if Ill be able to bind a property value of a view model to a XAML control.
XAML:
<Emtry x:Key="addressLine1" />
ViewModel:
public string addressLine1 { get; set; }
Is it possible to create a two way binding?
You will have to do it like this: <Entry Text="{Binding addressLine1, Mode=TwoWay}" />
The x:Key doesn't have much to do with it. You will have to bind to the property of the control that you want to use. In this case, on the Entry you want to bind it to the Text property so you can show it to the user and the user can edit it.
Then with the notation of {Binding addressLine1, Mode=TwoWay} you specify which property of the view model to bind to and what the mode should be. You can leave the mode out, then it will have the default value which is OneWay most of the time.
To make the connection between the XAML and the view model, you will still have to specify the DataBinding property on the code-behind of the XAML page.
<Entry x:Name="entAddress" Text="{Binding addressLine1}"/>

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.

Panorama Title binding

I'm doing WP7 app using Panorama control and have a problem with binding into Panorama Title property. Is it possible to bind that value out from ViewModel object?
Binding in xaml file:
<controls:Panorama x:Name="prmPanorama" Title="{Binding Voyage.Title}">
Voyage property of ViewModel is a Model entity (with Title property inside) with OnNotifyPropertyChanged event fired every time it changes:
private Voyage _voyage;
public Voyage Voyage
{
get { return _voyage; }
set
{
if (_voyage != value)
{
_voyage = value;
OnNotifyPropertyChanged("Voyage");
}
}
}
When I bind the same property into another control, eg. TextBlock, binding works just fine:
<TextBlock Text="{Binding Voyage.Title}" />
The text shown in that text block is as it should be but on the same time panorama title is not binded right - it's collapsed.
Does anyone tried to do that kind of binding? I have no idea why it doesn't work.
<DataTemplate x:Key="TitleDataTemplate">
<TextBlock Text="{Binding}" />
</DataTemplate>
...
<controls:Panorama Title="{Binding Voyage.Title}"
TitleTemplate="{StaticResource TitleDataTemplate}">
The control template of the panorama control uses a content presenter to display whatever value the its title property has kind of like a button. When setting the title template property, you indirectly set the content template of the content presenter.
That is why you have to set the title property on the panorama control and then can use that value in your title template for binding. In other words its not enough to just bind to the title you have to give it a template.
Check out this link for more info

Resources