Forum Xamarin Xamarin.Forms

Setting Binding with value converter in code behind without property name

gvuksagvuksa RSMember ✭✭

Hi all,

I have a list view with items source set to ObservableRangeCollection in view model.

When I try to use this xaml in code behind:

Text="{Binding Property, Converter=....}"

It translates in code behind to:

view.SetBinding(TextProperty, new Binding(nameof(Model.Property), BindingMode.Default, new InstanceOfConverter));

But, how can I replicate Text="{Binding Converter=...}" in code behind?


  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    If I have understand the problem, it should be something like

    new MyValueConverterClass()
  • gvuksagvuksa RSMember ✭✭

    Yes, but that part is not a problem. How can I pass entire binding context as a value to converter?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You should not pass the entire binding context. It accept the property you are using in the binding. You can pass a objectparameter to do some other work

  • gvuksagvuksa RSMember ✭✭
    edited February 2018

    It is a binding context of a list view items, which means a list property in view model.

    Well I need to format text based on multiple properties, so I have no option to pass single property and a parameter. At the end I will create some additional properties in model, format them the code behind and use them without converters, but that does not seem like a very elegant solution.

    In xaml, I use Text="{Binding Converter=....}" and as a value of converter I have the entire binding context list. It seems logical that I should be able to do the same using code.

Sign In or Register to comment.