I'm trying to use the new dateTimePicker for Windows 8.1:
<DatePicker HorizontalAlignment="Left" Margin="401,245,0,0" Grid.Row="1"
VerticalAlignment="Top" Width="352" Date="{Binding personSingle.personDOB,Mode=TwoWay}"/>
When ever I change the date I don't get the value that I chose when I look at value for personDOB.
personDOB is of type DateTimeOffset
What do I need to do get the value that I choose?
Update:
<DatePicker x:Name="dtPick" HorizontalAlignment="Left" Margin="401,245,0,0" Grid.Row="1"
VerticalAlignment="Top" Width="352" DataContext="{Binding personSingle}"
Date="{Binding personSingle.personDOB.Date,Mode=TwoWay}"/>
I found the answer from this link:
http://bretstateham.com/binding-to-the-new-xaml-datepicker-and-timepicker-controls-to-the-same-datetime-value/
You need to write a converter to get this to work appropriately:
public class DateTimeToDateTimeOffsetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
try
{
DateTime date = (DateTime)value;
return new DateTimeOffset(date);
}
catch (Exception ex)
{
return DateTimeOffset.MinValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
try
{
DateTimeOffset dto = (DateTimeOffset)value;
return dto.DateTime;
}
catch (Exception ex)
{
return DateTime.MinValue;
}
}
}
As it was explained to me, it is better to throw an exception when converting then to use a default.
public class DateTimeToDateTimeOffsetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return new DateTimeOffset((DateTime)value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return ((DateTimeOffset)value).DateTime;
}
}
Related
I am writing a MAUI app. I am trying to use a converter. But for some reason, the converter is called twice, and that makes the app crash. Here is the code:
xaml:
<Picker Title="Choose Tile Thickness..."
ItemsSource="{Binding TileThicknessesMetric}"
ItemDisplayBinding="{Binding Value}"
SelectedItem="{Binding SelectedTileThickness}"
IsVisible="{Binding SelectedUnit, Converter={converters:SelectedUnitToBoolShowMetricConverter}, Mode=TwoWay}"
Style="{StaticResource PickerStyle}"
Grid.Row="2" Grid.Column="1" />
Converter:
class SelectedUnitToBoolShowMetricConverter : IValueConverter, IMarkupExtension
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((Unit)value).Code == UnitCode.Metric;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
As I mentioned, Convert() is called twice, and the second time value is null, which causes a null reference exception. I cannot figure out why it is called the second time.
The same code works as expected in a Xamarin.Forms app.
I have a Image in my xaml in which the source is a URL that Iam binding.The URL from json will be like this : "/images/Uploads/e0111.png". I have the URL in my Common values stored class as CommonValues.URL. How can I add this "CommonValues.URL" before the json at the time of binding? So that the source for Image will be http://example.com//images/Uploads/e0111.png.?
If you need Uri
var myUrl= new Uri(CommonValues.URL + "images/Uploads/e0111.png");
If string than
var myUrl=CommonValues.URL + "images/Uploads/e0111.png";
Or you can do it like this in your ViewModel or Page
public string Url => string.Format("{0}{1}", CommonValues.URL,"/images/Uploads/e0111.png");
Then in XAML:
<Button Text="{Binding Url}"/>
You can use a converter, which will allow you to reuse in all your views/application
public class UrlConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var test = value as string;
if (!string.IsNullOrEmpty(test))
{
return CommonValues.URL + test;
}
return false;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
#endregion
}
Then, in your page:
<ContentPage.Resources>
<ResourceDictionary>
<converter:UrlConverter x:Key="UrlConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Image Source="{Binding YourProperty, Converter={StaticResource UrlConverter}}"/>
Apparantly in Xamarin forms there isn't an option to use FallbackValue or TargetNullValue, how could I use a converter to accomplish the task?
I'm looking to have visibility default to null if the data binding object is null.
NullConverter.cs
public class NullConverter : IMarkupExtension, IValueConverter
{
public object IsNullValue { get; set; }
public object IsNotNullValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? IsNullValue : IsNotNullValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
XAML
<Entry Text="{Binding WhateverProperty}" />
<Button IsVisible="{Binding WhateverProperty, Converter={local:NullConverter IsNullValue=False, IsNotNullValue=True}}" />
For my testing, WhateverProperty was a string that was originally set to null, whenever I update the Entry, the button shows up. Of course, you can use it with any type of property.
This question already has an answer here:
Can't bind CalendarDatePicker to a model in Xaml UWP
(1 answer)
Closed 5 years ago.
I'm triying to bind a DateTime from the ViewModel to a calendarDatePicker, but it doesn't work. i've tried in a textbox and works.
This part works:
<TextBox Text="{Binding MyDate}" ></TextBox>
And this doesn't:
<CalendarDatePicker Date="{Binding MyDate}" />
This question was previously answered here on MSDN.
Per the article, you could use a Converter to convert the binding to a DateTimeOffset. The converter may look something like the following, which comes from this post (referenced by the MSDN answer):
public class DateTimeToDateTimeOffsetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
try
{
DateTime date = (DateTime)value;
return new DateTimeOffset(date);
}
catch (Exception ex)
{
return DateTimeOffset.MinValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
try
{
DateTimeOffset dto = (DateTimeOffset)value;
return dto.DateTime;
}
catch (Exception ex)
{
return DateTime.MinValue;
}
}
}
How to set credit card number format In xaml
Like
1234-1234-1234-1234
This can be easily achieved by using Value converters.
public class CreditCardNumberValueConverter : IValueConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var builder = new StringBuilder(Regex.Replace(value.ToString(), #"\D", ""));
foreach (var i in Enumerable.Range(0, builder.Length / 4).Reverse())
builder.Insert(4*i + 4, " ");
return builder.ToString().Trim();
}
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Regex.Replace(value.ToString(), #"\D", "");
}
}
After initialising this in Styles.cs, you can apply it to the Text property of the control in XAML as:
Text="{Binding CardNo, Converter={StaticResource CreditCardNumberValueConverter}}"
Likewise, Phone numbers too can also be formatted.
<Label Text="{Binding YourVmPropertyNameHere StringFormat='{0:0000-0000-0000-0000}'}">
This is assuming that your view model has already converted the number to an integer and set it to the property "YourVmPropertyNameHere".