How to access BindableProperties through {Binding ...}.

JohnPrassJohnPrass Member ✭✭

I have created a sample custom control with a BindableProperty:

public partial class ButtonToggleTextControl : ContentView
    {
     private int count = 0;

     public static readonly BindableProperty TextProperty = BindableProperty.Create(
                                                           nameof(Text), //Public name to use
                                                           typeof(string), //this type
                                                           typeof(ButtonToggleTextControl), //parent type (this control)
                                                           string.Empty); //default value

     public string Text
     {
         get { return (string)GetValue(TextProperty); }
         set { SetValue(TextProperty, value); }
     }


        public ButtonToggleTextControl()
        {
            InitializeComponent();
            this.BindingContext = this;
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Text = $"Pushed {++count} times";
        }
    }
<ContentView xmlns="xamarin.com/schemas/2014/forms" 
             xmlns:x="schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="xamarin.com/schemas/2014/forms/design"
             xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="BindingProperties.Controls.ButtonToggleTextControl">
  <ContentView.Content>
      <StackLayout>
          <Label Text="Click the Button" />
            <Label Text="{Binding Text}"/>
            <Button Text="Click Me to Toggle Text" Clicked="Button_Clicked"/>
        </StackLayout>
  </ContentView.Content>
</ContentView>

If I set the BindableProperty directly all works as expected
<controls:ButtonToggleTextControl Text="This is a test" Focused="ButtonToggleTextControl_Focused"></controls:ButtonToggleTextControl>

But if I try to bind the property to another property the Text property doesn’t contain the value in ControlText.
<controls:ButtonToggleTextControl Text="{Binding ControlText, Mode=TwoWay}" Focused="ButtonToggleTextControl_Focused"></controls:ButtonToggleTextControl>

I don’t think it’s a standard binding issue because I can bind ControlText to other controls and it works as expected
<Label Text="{Binding ControlText}"/>

Here is the containing class for reference

public partial class MainPage : ContentPage
    {
        private string _controlText;
        public string ControlText
        {
            get { return _controlText; }
            set { _controlText = value; OnPropertyChanged(); }
        }

        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = this;
            ControlText = "Initial Set";
        }

    }

<ContentPage xmlns="xamarin.com/schemas/2014/forms"
             xmlns:x="schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="xamarin.com/schemas/2014/forms/design"
             xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:controls="clr-namespace:BindingProperties.Controls"
             mc:Ignorable="d"
             x:Class="BindingProperties.MainPage">

    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <!-- Place new controls here -->
        <controls:ButtonToggleTextControl Text="{Binding ControlText, Mode=TwoWay}"></controls:ButtonToggleTextControl>
        <Label Text="{Binding ControlText}"/>
    </StackLayout>

</ContentPage>

--All namespaces above have the protocols removed because I can't post links yet--

To be clear, what I expect to happen is that I can set ControlText property in MainPage to a value, and it should update the Text property of ButtonToggleTextControl.

Can someone help by explaining what I’m missing, or what I’m doing wrong?

Any help will be greatly appreciated.

Answers

  • JohnPrassJohnPrass Member ✭✭

    I did a little more searching on this and found my answer in these forums. I can't post links to the particular post, but if you search for "Data binding and passing object to custom control"you can find the answer by @adamkemp. Or you can add the protocol to this Link: forums.xamarin.com/discussion/comment/244399#Comment_244399

Sign In or Register to comment.