Pass binding to ConverterParameter

markmunizmarkmuniz USUniversity ✭✭
edited July 2016 in Xamarin.Forms

I'm currently trying to modify a list of commands being passed to my view from my view model if the item in the list contains a certain value, however the ConverterParameter doesn't seem to recognize what is being passed to it. Below is what I have inside of the ListView DataTemplate:

<MyView Items="{Binding Path=BindingContext.Commands, Source={x:Reference Name=ExamplePage}, Converter={converters:MyItemsConverter}, ConverterParameter={Binding .}}">

Is it possible to set multiple bindings for a converter or pass a binding to the ConverterParameter?

Answers

  • JeanBrunierJeanBrunier USMember

    Hi, I do have the same problem.

  • NMackayNMackay GBInsider, University mod
  • Anil.ApexAnil.Apex USMember ✭✭
    edited January 2017

    I'm also facing the same problem of binding ConverterParameter of SelectedIndex in Picker's control. I wasted 3 hrs before searching on google & finding this.
    Is there a feature request on Xamarin's user voice site for supporting binding in ConverterParameter?

    @NMackay Workarounds suggested by you are for Binding to Converter. It's not for ConverterParameter.

  • RianDutraRianDutra USMember ✭✭

    I have the same problem.

  • ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭

    Same here

  • CaioshinCaioshin ITMember ✭✭

    good suggestion, it works for me!

  • miglrodrimiglrodri PTMember ✭✭

    @NamyslawSzymaniuk said:
    Ugly, but working solution, is to define something additional, like Label hidded IsVisible = False:

    (...)

    I encountered this problem today and this was mainly the solution i thought about. Then i found your solution. It's a little bit hacky but it's working.

    In my sample i have a listview. Foreach row i have an image. If it's the selected item i want it to have a specific image. If it's not selected i want it to have another specific image.

    <ListView
                SelectedItem="{Binding SelectedEquipment}" 
                x:Name="lstView" 
                ItemsSource="{Binding Items}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            (...)
                                        <Label Text="{Binding Id, StringFormat='{0:N}'}" x:Name="equipmentId" IsVisible="False" />
    
                                    <Image
                                            Source="{Binding SelectedItem, Source={x:Reference lstView},
                                                Converter={StaticResource EquipmentSelectedItemToImageSourceConverter},
                                                ConverterParameter={x:Reference equipmentId}}"
                                            />
    (...)
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    Then my converter:

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
    
            var equipment = value as EquipmentModel;
            var thisWrapper = parameter as Label;
    
           //(... Logic, return the specific image if it's the selected item ...)
    
        }
    
  • JohnHunter.8704JohnHunter.8704 GBMember ✭✭

    A possible alternative solution for anyone interested would be to pass the image itself in as the parameter and then in the converter cast the parameter into an image type and get its binding context?

  • Wonde_ManWonde_Man ETMember ✭✭

    @NamyslawSzymaniuk
    your solution helped me a lot even if its not the best way ........

  • My workaround is to give the control a name, or the parent control, and pass this control via a reference into the ConverterParameter:

        <Label x:Name="label"
               Text="{Binding Source={x:Reference This}, Path=Value.Label, Converter={StaticResource textsourceConverter}, ConverterParameter={x:Reference label}}"/>
    

    And inside your converter you can cast the parameter to a bindable object and get the bindingcontext:

        public class TextSourceConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (!(parameter is BindableObject bindableObject))
                    return string.Empty;
    
                bindableObject.BindingContext
        ...
    

    This worked in my case based on @NamyslawSzymaniuk answer, only less hacky imho.

    Hope this helps.

Sign In or Register to comment.