How to binding property in a behavior?

thiagomelotothiagomeloto Member ✭✭
edited July 16 in Xamarin.Forms

In xaml file:

<Picker 
        x:Name="picker_cadastronotificacao_tipotelefone" 
        Title="{x:Static local:AppResources.picker_cadastronotificacao_tipotelefone}"
         Style="{StaticResource Picker}"
         SelectedItem="{Binding TipoTelefoneSelecionado}">
        <Picker.Items>
                  <x:String>Celular</x:String>
                  <x:String>Telefone Fixo</x:String>
         </Picker.Items>
</Picker>

<Entry 
      x:Name="entry_cadastronotificacao_telefone" 
      Placeholder="{x:Static local:AppResources.entry_cadastronotificacao_telefone}" 
      Style="{StaticResource EntradasTexto}" 
      Text="{Binding Telefone}"
      Keyboard="Telephone"
      IsEnabled="{Binding HabilitarCampoTelefone}">

      <Entry.Behaviors>
             <behavior:MaskBehavior Mascara="{Binding MascaraTelefone}"/>
      </Entry.Behaviors>
</Entry>

In view model:

if (TipoTelefoneSelecionado == "Telefone Fixo")
      MascaraTelefone = "(XX) XXXX - XXXX";
else if(TipoTelefoneSelecionado == "Celular")
      MascaraTelefone = "(XX) XXXXX - XXXX";

Error: No property, bindable property, or event found for 'Mascara', or mismatching type between value and property.

Best Answer

Answers

  • thiagomelotothiagomeloto Member ✭✭
    public class MaskBehavior : Behavior<Entry>
        {
            public static readonly BindableProperty MascaraTelefone = BindableProperty.Create("Mascara", typeof(string), typeof(MaskBehavior), "(XX) XXXX-XXXX");
    
            public string Mascara
            {
                get => (string)GetValue(MascaraTelefone);
                set => SetValue(MascaraTelefone, value);
            }        
    
            protected override void OnAttachedTo(Entry entry)
            {
                entry.TextChanged += OnEntryTextChanged;
                base.OnAttachedTo(entry);
            }
    
            protected override void OnDetachingFrom(Entry entry)
            {
                entry.TextChanged -= OnEntryTextChanged;
                base.OnDetachingFrom(entry);
            }
    
            IDictionary<int, char> _positions;
    
            void SetPositions()
            {
                if (string.IsNullOrEmpty(Mascara))
                {
                    _positions = null;
                    return;
                }
    
                var list = new Dictionary<int, char>();
                for (var i = 0; i < Mascara.Length; i++)
                    if (Mascara[i] != 'X')
                        list.Add(i, Mascara[i]);
    
                _positions = list;
            }
    
            private void OnEntryTextChanged(object sender, TextChangedEventArgs args)
            {
                var entry = sender as Entry;
    
                var text = entry.Text;
    
                if (string.IsNullOrWhiteSpace(text) || _positions == null)
                    return;
    
                if (text.Length > Mascara.Length)
                {
                    entry.Text = text.Remove(text.Length - 1);
                    return;
                }
    
                foreach (var position in _positions)
                    if (text.Length >= position.Key + 1)
                    {
                        var value = position.Value.ToString();
                        if (text.Substring(position.Key, 1) != value)
                            text = text.Insert(position.Key, value);
                    }
    
                if (entry.Text != text)
                    entry.Text = text;
            }
        }
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭

    The name of the BindableProperty field must be {property name} + "Property", i.e. "MascaraProperty".

  • thiagomelotothiagomeloto Member ✭✭

    I changed the property name to "MascaraProperty".

Sign In or Register to comment.