I have created a converter which looks like this:
public class SecondsToHoursMinutesValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var t = TimeSpan.FromSeconds((int)value); return string.Format("{0:D2} hours {1:D2} minutes", t.Hours, t.Minutes); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Now I want to bind it with:
runtime.SetBinding(Label.TextProperty, new Binding("Runtime"), BindingMode.Default, new SecondsToHoursMinutesValueConverter(), null);
However, VS does not like it and says there is an error in that code:
Error 1 The type arguments for method 'Xamarin.Forms.BindableObjectExtensions.SetBinding(Xamarin.Forms.BindableObject, Xamarin.Forms.BindableProperty, System.Linq.Expressions.Expression<System.Func<TSource,object>>, Xamarin.Forms.BindingMode, Xamarin.Forms.IValueConverter, string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
\git\Xam.Forms.Mvx\CoolBeans\CoolBeans\Pages\DetailedMoviePage.cs 101 13 CoolBeans
Posts
i don't have my mac here to try it on codebehind , but here's how it's done in xaml:
converter:
usage in xaml:
add the namespace:
xmlns:local="clr-namespace:Meditation;assembly=Meditation"
create and add to your page StaticResources:
add to your binding:
<Grid VerticalOptions="End" HorizontalOptions="Center" IsVisible="{Binding IsVisible, Converter={StaticResource cnvInvert}}">
Yeah, but I don't use XAML.
ok try this:
That seems to do the trick. What is the IValueConverter argument in SetBinding for then, when is that used?
Good question @Cheesebaron , don't know either. Maybe @StephaneDelcroix or @JasonASmith could answer that one.
There are 2
SetBinding()
extension methods declared in addition ofBindableObject.SetBinding()
:when you try to do
the compiler resolves it as a call to the 3rd override (the number of arguments match) but fails, as that override is
And there's no way for this to work.
You can achieve what you expect by using the second overload, as @rmarinho pointed out:
or use the 3rd one:
or in short:
thanks
How do you provide the parameter "Expression<Func<TSource, object>> sourceProperty"
I have this piece of code:
I want to bind the entry text to address.State.
I know I can do something like this:
However, if I refactor the property "State" to "Province" for example, I have to go change the string instead of using the Rename feature of Visual Studio / Xamarin Studio. Therefore, I would like to use the Expression if it can solve the refactoring problem.
Thank you very much.
Never mind,
I figured it out, it will be
@rmarinho Would it also be possible to bind to an IValueConverter in the BindingContext instead of using StaticResource? Like this?
I tried doing this, but I get the following error when I try binding in XAML, but it works when I setup up the binding in the code behind.
System.Exception: A Binding Converter must be of type IValueConverter.
No, that's not possible. Converter is not bindable, AND you can't change a binding
@Cheesebaron Hi, I am trying to implement Input validation in Xamarin.Forms using Behaviors. I am not using XAML. I want to do this in code behind. I have most of it working except this - I have seen a sample where when a user starts typing a number in the Entry and a label below the entry appears in "green" if the number is valid, but if not then the label becomes red. Any suggestions? Thanks
I know this is old but if anyone runs across this and wants to be able to use a ValueConverter, like a MarkupExtension without the StaticResource declaration, the answer is here.
http://stackoverflow.com/questions/7445119/improved-ivalueconverter-markupextension-or-dependencyobject
Simply add the IMarkupExtension inheritance and the ProvideValue method and you're all set:
Sorry for hijacking this old post! Maybe I do not do it correctly. When I use {Binding .} I got in my converter the ViewModel directly. But what I need is the object of the collection bound to the listview.
For example, I got an observableCollection of Calls. In that collection I got 2 field DateStart, DateEnd.
In my listview I need a column that need to use theses 2 dates calculate something and return a string. So my "root" need the Call object in the Calls collection currently being drawn instead of the whole viewModel I got with {Binding .}. Do we have a way to do that?
@PierreSavard I think a solution can be to create a new Property in your Model that return the string to visualize in the ListView