How to do the equivalent of {Binding Source} in code?

TC2254TC2254 USMember
edited March 2016 in Xamarin.Forms

I'm extending a control to be able to reuse it across my current Xamarin project. As part of this control, I need to create a DataTemplate programmatically. I have this part figured out and it works ok.

The DataTemplate has a Label in it. I need to bind the Label's BindingContext property to {Binding Source}. I need to bind the Label's Text property to {Binding Path=Name}.

This works in XAML, but I don't want to have to copy it to a million different places in the code base.
<dxGrid:TemplateColumn FieldName="MyPropertyName" Caption="MyColumn"> <dxGrid:TemplateColumn.DisplayTemplate> <DataTemplate> <Label BindingContext="{Binding Source}" Text="{Binding Source, Path=MyPropertyName}" /> </DataTemplate> </dxGrid:TemplateColumn.DisplayTemplate>

My extended control looks like this right now:
`public class MyColumn : TemplateColumn
{
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
BindingBase textBinding = new Binding(FieldName);

            Label label = new Label();

            // TODO: Bind BindingContextProperty to {Binding Source}
            //label.SetBinding(BindingContextProperty, binding);

            label.SetBinding(Label.TextProperty, textBinding);
            return new ViewCell
            {
                View = label
            };
        });

        DisplayTemplate = displayTemplate;
    }
}`

I'm getting hung up in the binding because I'm not sure how to do the equivalent of {Binding Source} in code. Any help would be appreciated.

This question was originally posted to StackOverflow here: http://stackoverflow.com/questions/35808706/how-to-do-the-equivalent-of-binding-source-in-code

Tagged:

Posts

  • TC2254TC2254 USMember
    edited March 2016

    @AdamP Thanks for the response. Unfortunately this does not work as there is no "Source" property on the TemplateColumn. If I set it to a string "Source", then it fails with a Null Reference Exception. I made another pass at it this morning and got it working this way:

    public MyColumn()
    {
        DataTemplate displayTemplate = new DataTemplate(() =>
        {
            Grid grid = new Grid();
            grid.SetBinding(Grid.BindingContextProperty, "Source");
    
            Label label = new Label();
            label.SetBinding(Label.TextProperty,FieldName);
    
            grid.Children.Add(label);
    
            return grid;
        });
    
        this.DisplayTemplate = displayTemplate;
    }
    
  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @TC2254 - glad you managed to get it working. Apologies, I missed the fact it was in a DataTemplate.

  • ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭

    Is there also any way to perform GetBinding? I want to get the actual Binding (i.e. the property name), not the value.
    Anyone?

  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited December 2017

    @ShimmyWeitzhandler - Neither BindingContext nor BindableObject have any methods that would help you search for a binding, so I believe the answer is "No". AFAIK, you would have to create the bindings in code (not XAML), and create your own association (dictionary) between views (or viewmodels?) and binding names. Maybe you can create and cache the BindableProperties involved in the bindings?

  • GaetanFGaetanF USMember ✭✭✭

    @SteveShaw.5557 - You actually CAN create a binding in XAML and use it later. It can be cached in the resource dictionary (which I recommend if you are switching bindings at runtime) or created in place. Here is a simple example:

    <Binding x:Key="MyKeyIfInResDic" Path="The.Property" Converter="{StaticResource MyStaticConverter}" />

    If you are following the MVVM principles, no ViewModel should ever have to bother with creating/setting/caching bindings, if you'd ask me.

Sign In or Register to comment.