Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Bindable Property on Custom Control Error

$
0
0

Hi,

I have a custom control for entry, and Binding for the text is not working. It works before this, and now it's not working anymore. I have no idea why it stopped working.

EntryBinding.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="TestingApp.EntryBinding">
    <Entry Text="{Binding Path=MyText, Mode=TwoWay}"/>
</Grid>

In my EntryBinding I have a BindableProperty called MyText

public static readonly BindableProperty MyTextProperty = BindableProperty.Create(
    "MyText",
    typeof(string),
    typeof(EntryBinding),
    "",
    propertyChanged: OnMyTextChanged);

private static void OnMyTextChanged(BindableObject obj, object oldValue, object newValue)
{
    var item = obj as EntryBinding;
    var newV = (string)newValue;
    var oldV = (string)oldValue;
}

public string MyText
{
    get
    {
        return (string)GetValue(MyTextProperty);
    }
    set
    {
        SetValue(MyTextProperty, value);
    }
}

Then I tried to use the custom control in my MainPage like this:
<local:EntryBinding MyText="{Binding TheText}"/>

However, when I run, I receive this error: Binding: 'TheText' property not found on 'TestingApp.MainPage', target property: 'TestingApp.MYCB.MyText'

Below is my MainPage and my Items class

public partial class MainPage : ContentPage
{
    Items items = new Items();

    public MainPage()
    {
        InitializeComponent();

        BindingContext = items;
    }

    void Handle_Clicked_2(object sender, System.EventArgs e)
    {
        items.TheText = "WOW";
    }
}

public class Items : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    string _TheText = "";
    public string TheText
    {
        get { return _TheText; }
        set
        {
            _TheText = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TheText"));
        }
    }
}

I don't know where went wrong. It run perfectly before this, and now it just have the error I mentioned above.

Thanks in advance,
AziziAziz


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>