Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Created June 30, 2020 13:30
Show Gist options
  • Save sjehutch/37c4d3494ca6e721d8265a7478d7ecfc to your computer and use it in GitHub Desktop.
Save sjehutch/37c4d3494ca6e721d8265a7478d7ecfc to your computer and use it in GitHub Desktop.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
namespace EntryCompleteEvent
{
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel()
{
IsControlVisible = true;
CompletedCommand = new Command(_ => CompletedCommandExecuted(_));
}
private bool _isControlVisible;
public bool IsControlVisible
{
get { return _isControlVisible; }
set
{
_isControlVisible = value;
OnPropertyChanged();
}
}
public System.Windows.Input.ICommand CompletedCommand { get; protected set; }
private void CompletedCommandExecuted(object param)
{
IsControlVisible = false;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:EntryCompleteEvent"
x:Class="EntryCompleteEvent.EntryCompleteEventPage">
<StackLayout>
<Entry Placeholder="Email Address"
Keyboard="Email"
x:Name="EmailEntry"
IsVisible="{Binding IsControlVisible}">
<Entry.Behaviors>
<local:EntryCompletedBehavior Command="{Binding CompletedCommand}" />
</Entry.Behaviors>
</Entry>
</StackLayout>
</ContentPage>
using System;
using System.Reflection;
using System.Windows.Input;
using Xamarin.Forms;
namespace EntryCompleteEvent
{
public class EntryCompletedBehavior : Behavior<Entry>
{
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
bindable.BindingContextChanged += Bindable_BindingContextChanged;
bindable.Completed += Bindable_Completed;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= Bindable_BindingContextChanged;
bindable.Completed -= Bindable_Completed;
AssociatedObject = null;
}
private void Bindable_BindingContextChanged(object sender, System.EventArgs e)
{
OnBindingContextChanged();
}
private void Bindable_Completed(object sender, System.EventArgs e)
{
if (Command == null)return;
//object parameter = Converter.Convert(e, typeof(object), null, null);
if (Command.CanExecute(e))
{
Command.Execute(e);
}
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
public Entry AssociatedObject { get; private set; }
public static readonly BindableProperty CommandProperty =
BindableProperty.Create("Command", typeof(ICommand), typeof(EntryCompletedBehavior), null);
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment