1

Criei um CustomControl porém criando uma BindingProperty não consigo efetuar um binding

Código XAML do Custom Control

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="CustomControls.Controls.CustomEditor">
    <Editor x:Name="edt" Text="{Binding Text, Mode=TwoWay}" />
</ContentView>

Código CS do CustomControl

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomEditor : ContentView
{
    public CustomEditor ()
    {
        InitializeComponent ();
        this.BindingContext = this;
    }

    public static readonly BindableProperty TextProperty = BindableProperty.Create(
       propertyName: nameof(Text),
       returnType: typeof(string),
       declaringType: typeof(CustomEditor),
       defaultValue: "",
       defaultBindingMode: BindingMode.TwoWay,
       propertyChanged: (b, o, n) =>
       {
           if (b is CustomEditor view)
               view.edt.Text = n.ToString();
       });        

    public string Text
    {
        get => (string)GetValue(TextProperty);
        set
        {
            SetValue(TextProperty, value);
            base.OnPropertyChanged(nameof(Text));
        }
    }
}

MainPage XAML que consome o CustomControl

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:CustomControls"
         xmlns:controls="clr-namespace:CustomControls.Controls"
         x:Class="CustomControls.MainPage">
    <StackLayout Padding="10">
        <Label Text="{Binding Email}"/>
        <Editor x:Name="txtTeste" Text="{Binding Email}"/>
        <controls:CustomEditor Text="{Binding Email, Mode=TwoWay}"/>
    </StackLayout>
</ContentPage>

MainPage CS

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = this;
        this.Email = "[email protected]";
    }

    private string _email;

    public string Email
    {
        get { return _email; }
        set { _email = value; base.OnPropertyChanged(nameof(Email)); }
    }
}

Na MainPage no customControl se começo com um HardCode ele já abre o app com Editor preenchido porém qualquer alteração na propriedade Email ele não reflete no Editor e se mudo o valor do Editor ele não reflete na propriedade email, é possível utilizar Binding nessa Situação?

1 Resposta 1

1

Com esse artigo CustomControls Xamarin Forms consegui resolver meu problema, o componente final ficou.

XAML

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="Camareira.Controls.FormEntry" 
        HorizontalOptions="FillAndExpand">
    <Grid.RowDefinitions>
        <RowDefinition Height="25"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label x:Name="label" Grid.Row="0"/>
    <Entry x:Name="entry" Grid.Row="1"/>
</Grid>

.CS

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class FormEntry : Grid
{
    public FormEntry ()
    {
        InitializeComponent ();
        entry.TextChanged += (s, e) =>
        {
            Text = e.NewTextValue;
        };
    }

    public static readonly BindableProperty TitleProperty = BindableProperty.Create(
        nameof(Title),
        typeof(string),
        typeof(FormEntry),
        null,
        BindingMode.OneWay);
    public string Title
    {
        get => (string)GetValue(TitleProperty);
        set => SetValue(TitleProperty, value);
    }

    public static readonly BindableProperty TextProperty = BindableProperty.Create(
        nameof(Text),
        typeof(string),
        typeof(FormEntry),
        null,
        BindingMode.TwoWay);
    public string Text
    {
        get => (string)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }

    public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(
        nameof(Placeholder),
        typeof(string),
        typeof(FormEntry),
        null,
        BindingMode.TwoWay);
    public string Placeholder
    {
        get => (string)GetValue(PlaceholderProperty);
        set => SetValue(PlaceholderProperty, value);
    }

    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);
        switch (propertyName)
        {
            case nameof(Title):
                label.Text = Title;
                break;
            case nameof(Text):
                entry.Text = Text;
                label.IsVisible = !String.IsNullOrEmpty(Text);
                break;
            case nameof(Placeholder):
                entry.Placeholder = Placeholder;
                break;
        }
    }
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .