Skip to content

Instantly share code, notes, and snippets.

@Char0394
Created June 2, 2020 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Char0394/05e5c8b2e7347ef3fa70f64e3b7109ba to your computer and use it in GitHub Desktop.
Save Char0394/05e5c8b2e7347ef3fa70f64e3b7109ba to your computer and use it in GitHub Desktop.
{
public class EntryLineValidationBehaviour : BehaviorBase<Entry>
{
#region StaticFields
public static readonly BindableProperty IsValidProperty = BindableProperty.Create(nameof(IsValid), typeof(bool), typeof(EntryLineValidationBehaviour), true, BindingMode.Default, null, (bindable, oldValue, newValue) => OnIsValidChanged(bindable, newValue));
#endregion
#region Properties
public bool IsValid
{
get
{
return (bool)GetValue(IsValidProperty);
}
set
{
SetValue(IsValidProperty, value);
}
}
#endregion
#region StaticMethods
private static void OnIsValidChanged(BindableObject bindable, object newValue)
{
if (bindable is EntryLineValidationBehaviour IsValidBehavior &&
newValue is bool IsValid)
{
IsValidBehavior.AssociatedObject.PlaceholderColor = IsValid ? Color.Default : Color.Red;
}
}
#endregion
}
public class BehaviorBase<T> : Behavior<T>
where T : BindableObject
{
#region Properties
public T AssociatedObject
{
get;
private set;
}
#endregion
#region NormalMethods
private void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}
#endregion
#region Overrides
protected override void OnAttachedTo(T bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
if (bindable.BindingContext != null)
{
BindingContext = bindable.BindingContext;
}
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom(T bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment