Binding controls to a property on the Page

I've got a page which has a property. At the moment I'm setting up the page in the code behind but want to clean up the solution and use data binding to the property items and having the data binding in XAML.

<ContentPage.Content >
    <StackLayout Spacing="12" Padding="30,0"   >

        <input:RadioButtonGroupView  x:Name="RBG1" SelectedItemChanged="RBG1_SelectedItemChanged" >
            <input:RadioButton x:Name="RB1" Text="RB2" TextFontSize="20"  BackgroundColor="red" TextColor="White"  />
            <input:RadioButton x:Name="RB2" Text="Option 2" TextFontSize="20"  BackgroundColor="silver" TextColor="black" />
            <input:RadioButton x:Name="RB3" Text="Option 3" TextFontSize="20"  BackgroundColor="green" TextColor="White"  />
            <input:RadioButton x:Name="RB4" Text="Option 4" TextFontSize="20"  BackgroundColor="blue" TextColor="White" />
        </input:RadioButtonGroupView>

        <Button Text="{Binding Source=MyProperty,Path=MyProperty1  }"></Button>
    </StackLayout>
</ContentPage.Content>

and the code behind is

public partial class RadioGroup : ContentPage
{
    public Class1 MyProperty { get; set; }

    public RadioGroup ()
    {
        InitializeComponent ();
        //RBG1.BindingContext("")

        MyProperty = new Class1();  //Just for demo purposes to set the property


        RB1.Text = MyProperty.MyProperty1;
        RB2.Text = MyProperty.MyProperty2;
        RB3.Text = MyProperty.MyProperty3;
        RB4.Text = MyProperty.MyProperty4;        
    }        

}

So I'm setting the Text attribute on RB1,2,3,4 in the code behind but and trying to do it using something in the XAML

but can't seem to find any simple example of this - any assistance ?

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
  • AdrianBowlesAdrianBowles USMember ✭✭

    I'd seen an example and got it working but am a little confused as to what the 3rd parameter of the Create method is

    declaringType
    The type of the declaring object.

    I looked at the documentation and it didnt tell me much but changed the example to the control I wanted to bind to ie. RadioButton and I got it to work but do I then need to create various items if I want to bind sub properties in the property to different controls.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @AdrianBowles said:
    am a little confused as to what the 3rd parameter of the Create method is

    what are you talking about? What create method?

    You have a property on your ViewModel. You set it as the binding target in your XAML. Done.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    BindableProperty.Create()

    The declaringType parameter is just the type of the class you're writing the code in, so it would be typeof(RadioGroup) in your case. Honestly I don't really know what the point of it is.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Well that's the thing... the OP isn't showing any code where he's using it. I pointed him at tutorials that don't use it. There is no attempt at actually doing binding for his RadioButton in his XAML. I'm not sure what anyone is supposed to go by in an effort to help him.
    cc: @JoeManke

  • AdrianBowlesAdrianBowles USMember ✭✭

    No to be annoying but Clint you did in fact point me to a simple solution on your pages and your example did in fact call a create method for the BindableProperty class. ( BindableProperty.Create() ) Which ultimately is a key point in solving the problem.

    http://redpillxamarin.com/2017/01/28/206-reusable-controls/

    Extract from your own example:

    public static readonly BindableProperty CaptionProperty = BindableProperty.Create(
    "Caption", //Public name to use
    typeof(string), //this type
    typeof(InputCntrl), //parent type (tihs control)
    string.Empty); //default value
    public string Caption
    {
    get { return (string)GetValue(CaptionProperty); }
    set { SetValue(CaptionProperty, value); }
    }

    Joe, appeared to understand what I was talking about - and I realized I could put in either RadioButton or the Page class and it would work. So I wasn't sure what this was talking about and why it was specifically required but I got something working. The documentation for the 3rd parameter on the BindableProperty.Create method is really general and doesnt detail much, hence the follow up question.

Sign In or Register to comment.