Conditional databinding to a TimeSpan? - windows-phone-7

I'm trying to bind a TextBlock to a TimeSpan, but I need to format is so that if TotalMinutes is less then 60 it should show "X min" else it should show "X h".
Its it possible? That might require tom logic test in the xaml?

You should use custom IValueConverter implementation. There are several tutorials about that, e.g. Data Binding using IValueConverter in Silverlight.
Your IValueConverter implementation should look like that:
public class TimeSpanToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is TimeSpan))
throw new ArgumentException("value has to be TimeSpan", "value");
var timespan = (TimeSpan) value;
if (timespan.TotalMinutes > 60)
return string.Format("{0} h", timespan.Hours.ToString());
return string.Format("{0} m", timespan.Minutes.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

Related

How to pass value from xamarin page to ivalueconverter class properties Xamarin Forms

I have a class of IValueConverter with a property named myValue which I want to divide the ivalueconverter by its property myValue! But, I want to know if it is possible to pass the myValue from xamarin page to ivalueconverter? If yes, how?
IvalueConverter
class salesUIbtnWidth : IValueConverter
{
public double myValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value / myValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Try Implementing the class
<Button Text="Click me" HorizontalOptions="Center" VerticalOptions="Center"
CornerRadius="15" WidthRequest="{Binding Source={x:Reference
frame}, Path=Width,Converter={StaticResource salesUIbtnWidth}}">
</Button>//how to bind also the MyValue
All I want is to know how to set myValue in page and pass it to (IvalueConverter) from xamarin during runtime!!
You can pass the value by ConverterParameter.
You can refer to the sample code:
<Label Text="{Binding Red,
Converter={StaticResource doubleToInt},
ConverterParameter=255,
StringFormat='Red = {0:X2}'}" />
<local:DoubleToIntConverter x:Key="doubleToInt" />
DoubleToIntConverter.cs
public class DoubleToIntConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)Math.Round((double)value * GetParameter(parameter));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value / GetParameter(parameter);
}
double GetParameter(object parameter)
{
if (parameter is double)
return (double)parameter;
else if (parameter is int)
return (int)parameter;
else if (parameter is string)
return double.Parse((string)parameter);
return 1;
}
}
For more, you can check: Binding Converter Parameters
According to your question this is what you need to do!
Following #Jessie Zhang example
<Button HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
HeightRequest="{Binding Source={x:Reference frame}, Path=Height,
Converter={StaticResource doubleToInt}, ConverterParameter=enterYOURvaluHERE}"></Button>
If you don't want to use parameter, you can define MyValue when you create the converter.
<ContentPage.Resources>
<app1:MyConverter x:Key="myConverter" MyValue="255"/>
</ContentPage.Resources>

Hiding Xamarin Forms Shell SearchHandler

I have implemented a Xamarin Forms SearchHandler in my Shell project. I want to be able to "hide" it (and unhide it). There is no IsVisible property on it - any ideas how to hide the SearchHandler control?
I figured it out - the property is actually SearchBarVisibility.
In my case, I created a value converter to and bound SearchBarVisibility to a property in my ViewModel.
My ValueConverter looks like this:
public class SearchVisibleConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SearchBoxVisibility searchBoxVisibility = SearchBoxVisibility.Expanded;
if (value != null)
{
switch (value)
{
case "Hidden":
searchBoxVisibility = SearchBoxVisibility.Hidden;
break;
case "Collapsible":
searchBoxVisibility = SearchBoxVisibility.Collapsible;
break;
default:
searchBoxVisibility = SearchBoxVisibility.Expanded;
break;
}
}
return searchBoxVisibility;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
My xaml looks like this:
<Shell.SearchHandler>
<controls:RouteSearchHandler
x:Name="RouteSearch"
BackgroundColor="White"
DisplayMemberName="Street1"
SearchBoxVisibility="{Binding TopSearchVisibility, Converter={StaticResource visibleConvert}}"
ShowsResults="True" />
</Shell.SearchHandler>

How to set DatePicker value equal yesterday in xaml in xamarin.form?

To set the Date attribute in xaml equal today we can use Today attribute as following:
xmlns:system="clr-namespace:System;assembly=mscorlib"
<DatePicker Date="{x:Static system:DateTime.Today}"/>
I've tried to set it by using AddDays method but it couldn't help.
<DatePicker Date="{x:Static system:DateTime.Today.AddDays(-1)}"/> Does not work.
So is there a way?
After some discussions the best answer I have found is using a helper like following:
In xaml:
xmlns:helpers="clr-namespace:Helpers"
<DatePicker Date="{x:Static helpers:DateTimeHelper.Yesterday}"/>
While helper is:
namespace Helpers
{
public class DateTimeHelper {
public static System.DateTime Yesterday
{
get { return System.DateTime.Today.AddDays(-1); }
}
}
}
You can set the date from the controller.
<DatePicker x:Name="PostingDatePicker" />
PostingDatePicker.Date = DateTime.Today.AddDays(-1);
Another way is that you bind the date from your ViewModel,
Xaml
<DatePicker x:Name="PostingDatePicker" Date="{Binding PostingDate}"/>
ViewModel
public DateTime PostingDate {get; set;}
PostingDate = DateTime.Today.AddDays(-1);
Updated Answer:
If you want to update the date only through XAML, you can use converters,
<DatePicker x:Name="PostingDatePicker" Date="{Binding PostingDate, Converter={local:DateConverter}}"/>
create a class which inherits IValueConverter,
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
return DateTime.Today.AddDays(-1);
}
catch (Exception)
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
At runtime, your datepicker date will be updated with yesterday's date.

Object to string ValueConverter not getting invoked

I have defined a Value Converter in my PCL like this:
class EmployeeToStringValueConverter : MvxValueConverter<Employee, string>
{
protected override string Convert(Employee value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}
}
And I'm using it in an Android .axml layout like this:
local:MvxBind="Text EmployeeToString(SearchResult)"
(SearchResult is defined in the ViewModel as a public Employee property)
But it is not working (meaning: if I put a breakpoint in the Converter call, it is never executed).
However, I've also defined the following converter:
public class NegateBoolValueConverter : MvxValueConverter<bool, bool>
{
protected override bool Convert(bool value, Type targetType, object parameter, CultureInfo culture)
{
return !value;
}
}
And I'm using it like so:
local:MvxBind="Enabled NegateBool(IsLoggedIn)"
(IsLoggedIn is a bool public property in the ViewModel)
And this works perfectly.. Any idea so as to what may be going on with the first one that's not working?
I believe you are missing a public access modifier on your converter
Changing
class EmployeeToStringValueConverter : MvxValueConverter<Employee, string>
to
public class EmployeeToStringValueConverter : MvxValueConverter<Employee, string>
should do the trick.

Itemdatabound event of longlistselector control in Windows phone 7

I want to control the visibility of an image (inside a dynamically bind longlistselector control), depending on a binding value (say if somevalue>0 then make that image visible otherwise invisible).But there is no such event like itemdatabound in the longlistselector to accomplish this task, I am new to windows phone development, and really don't have an idea how to do this.Please help me guys.
Thanks,
Use a ValueConverter
public class BoolToVisiblityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value
? Visibility.Collapsed
: Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
A common way to achieve this would be to bind the visibility property of the Image to a property on your bound data object. Often, the property on the data would be a boolean and a converter would be used to convert the boolean to a Visibility enum value. E.g.,
<Image Visibility = {Binding IsVisible, Converter={StaticResource myBoolToVisibilityConverter} />
See binding with converters example

Resources