Bound element being called in C#

Hello, first off sorry I am new to Xamarin and VERY noobish, any and all detail will go a long way in helping me resolve this issue. =)

Basically I have an item being sent to another page:
void OnItemSelected (object sender, SelectedItemChangedEventArgs e)
{
var todoItem = e.SelectedItem as AdEvent;
var todoPage = new AdEventItemPageScroll();
todoPage.BindingContext = todoItem;
Navigation.PushAsync (todoPage);
}

On that page it is being seen using the binding:
x:Name="event_name" Text="{Binding Path=EventName}"

What I am attempting to do it this same function through C#, something like the below example:
event_name.Text = BoundItem.EventName

I am unsure how to go about doing this and have been Googling for a while. =(
ANY help would be extremely appreciated. =D

Best Answers

  • JoeMankeJoeManke US ✭✭✭✭✭
    Accepted Answer

    event_name.SetBinding(Label.TextProperty, "EventName");

    In C# code, you use the BindableObject.SetBinding method. All Views inherit from BindableObject. The official API takes a BindingBase parameter, but you would actually use the Binding subclass since BindingBase is abstract. There are also extension methods available so you can just give SetBinding the arguments you would give to the Binding constructor. At the very least you need the property name, but you can also specify the binding mode, converter, and string format if necessary.

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    Accepted Answer

    event_name.SetBinding(Label.TextProperty, "EventName");

    In C# code, you use the BindableObject.SetBinding method. All Views inherit from BindableObject. The official API takes a BindingBase parameter, but you would actually use the Binding subclass since BindingBase is abstract. There are also extension methods available so you can just give SetBinding the arguments you would give to the Binding constructor. At the very least you need the property name, but you can also specify the binding mode, converter, and string format if necessary.

  • clintonboozeclintonbooze USMember ✭✭

    @JoeManke Thank you so much for the reply, that is exactly what I was looking for. Funny enough I found a similar snip-it in the FormsGallery example on the NamedColorPage.cs provided by Xamarin.
    The additional detail you provided actuallly does help me understand it. lol =D

  • clintonboozeclintonbooze USMember ✭✭

    Is there a way to possibly save it to a variable? Because for example I have several pieces consisting of the address/city/state/etc..
    I would like to concat them before assigning the text to the label.
    Something like String foo = SetBinding(String.Value,"EventAddress");
    I am finding some stuff about StringFormat for binding but am unsure how to implement, I will continue researching. =P

Sign In or Register to comment.