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 mod 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 ✭✭
  • NamyslawSzymaniukNamyslawSzymaniuk ✭✭✭✭ USMember ✭✭✭✭
  • Wonde_ManWonde_Man ✭✭ ETMember ✭✭

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

  • Paul_de_BoerPaul_de_Boer Member

    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.

  • ghasanghasan ✭✭ Member ✭✭
    edited April 25

    I see, this is because ConverterParameter is not a BindableProperty.

    https://github.com/xamarin/Xamarin.Forms/blob/4fd2d6b45b7f287433ea2bf52581d376a06b999f/Xamarin.Forms.Xaml/MarkupExtensions/BindingExtension.cs#L13

    It would have been nice if ReferenceExtension had a Path property, so in the converters we don't receive ugly views and access their binding context manually.

  • NickKovalskyNickKovalsky ✭✭✭ USMember ✭✭✭

    Due to recent xamarin.forms optimizations properties not affecting visibility are not receiving values from bindingcontext if the control is not visible. So for the mentioned label hack to work set it to be visible, just empty.

  • Harrison_NguyenHarrison_Nguyen ✭✭ Member ✭✭

    @ghasan said:
    I see, this is because ConverterParameter is not a BindableProperty.

    https://github.com/xamarin/Xamarin.Forms/blob/4fd2d6b45b7f287433ea2bf52581d376a06b999f/Xamarin.Forms.Xaml/MarkupExtensions/BindingExtension.cs#L13

    It would have been nice if ReferenceExtension had a Path property, so in the converters we don't receive ugly views and access their binding context manually.

    So, can we bind data to ConverterParameter now ?

  • AdamLangleyAdamLangley ✭✭ NZMember ✭✭

    For anyone still watching this topic - you can just use converter properties instead of the ConverterParameter.
    Read the section on "Binding Converter Properties":

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters

    Derive your IValueConverter from BindableObject, and throw your dependency properties into it - and it works a treat.

Sign In or Register to comment.