public static class BindableObjectExtensions
{
// chainable, with a string for property
// usage : new Button().WithBinding(Button.CommandProperty, "SomeCommand"),
public static T WithBinding<T>(this T obj,
BindableProperty bindableProperty,
string path,
BindingMode mode = BindingMode.Default,
IValueConverter converter = null,
object converterParameter = null,
string stringFormat = null
)
where T:BindableObject
{
obj.SetBinding (bindableProperty, new Binding (path, mode, converter, converterParameter, stringFormat));
return obj;
}
// chainable, with a lambda for property
// usage : new Button().WithBinding(Button.CommandProperty, (MyViewModel vm) => vm.SomeCommand),
public static TOUT WithBinding<TOUT, TVM, TO>(this TOUT obj,
BindableProperty bindableProperty,
Expression<Func<TVM, TO>> sourceProperty,
BindingMode mode = BindingMode.Default,
IValueConverter converter = null,
object converterParameter = null,
string stringFormat = null
)
where TOUT:BindableObject
where TVM:INotifyPropertyChanged
{
obj.SetBinding (bindableProperty, new Binding (((MemberExpression)sourceProperty.Body).Member.Name, mode, converter, converterParameter, stringFormat));
return obj;
}
}
Usage :
new StackLayout{
Children =
{
new Button().WithBinding(Button.CommandProperty, (MyViewModel vm) => vm.SomeCommand),
},
}
1 comment:
a hacky way to make it support ViewModels in ViewModels :
var body = sourceProperty.Body.ToString ();
string name;
if (body.Contains ("."))
name = body.Substring (body.IndexOf (".", StringComparison.Ordinal) + 1);
else
name = body;
obj.SetBinding (bindableProperty, new Binding (name, mode, converter, converterParameter, stringFormat));
Post a Comment