dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

Binding a converter parameter to a viewmodel object returns bindable property for that object in the converter and not the value. #8371

Open Awesomer9561 opened 2 years ago

Awesomer9561 commented 2 years ago

Description

Hi, I am trying to bind a background color using converter and converter parameter but I am not able to pass ViewModel objects as parameters. Any ideas?

Steps to Reproduce

<Button x:Name="button_category" Text="{Binding Title}" CornerRadius="25"
                            Padding="20, 10" MinimumWidthRequest="80" Style="{StaticResource TextAppearance.12.Regular}"
                            Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:ManageLeadsViewModel}}, Path=SwitchCategoryCommand}">
                            <Button.BackgroundColor>
                                <Binding Path="IsSelected" ConverterParameter="IsSelected">
                                    <Binding.Converter>
                                        <converters:TypedBoolConverters x:TypeArguments="Color"
                                                                    TrueObject="{StaticResource Purple}"
                                                                    FalseObject="{StaticResource FadedGreen}"/>
                                    </Binding.Converter>
                                </Binding>
                            </Button.BackgroundColor>
                            <Button.TextColor>
                                <Binding Path="IsSelected" ConverterParameter="IsSelected">
                                    <Binding.Converter>
                                        <converters:TypedBoolConverters x:TypeArguments="Color"
                                                                    TrueObject="{StaticResource White}"
                                                                    FalseObject="{StaticResource Purple}"/>
                                    </Binding.Converter>
                                </Binding>
                            </Button.TextColor>
                        </Button>

Version with bug

6.0.400 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

iOS 15, Android API 30, 31

Did you find any workaround?

No response

Relevant log output

No response

AndreasReitberger commented 2 years ago

Can you try to define the Convertes in the ContentPage resource instead?

    <ContentPage.Resources>
        <converters:UnixDoubleHoursToTimeSpanConverter x:Key="UnixDoubleHoursToTimeSpanConverter" />
    </ContentPage.Resources>

Then, bind the property like this.

Text="{Binding RemainingPrintTime, Converter={StaticResource UnixDoubleHoursToTimeSpanConverter}}"

I do this way and have no problems with Converters.

Awesomer9561 commented 2 years ago

Can you try to define the Convertes in the ContentPage resource instead?

    <ContentPage.Resources>
        <converters:UnixDoubleHoursToTimeSpanConverter x:Key="UnixDoubleHoursToTimeSpanConverter" />
    </ContentPage.Resources>

Then, bind the property like this.

Text="{Binding RemainingPrintTime, Converter={StaticResource UnixDoubleHoursToTimeSpanConverter}}"

I do this way and have no problems with Converters.

That is working fine but passing parameters is an issue.

AndreasReitberger commented 2 years ago

You can define the Paramters in here.

 <ContentPage.Resources>
        <converters:TypedBoolConverters x:Key="FirstConverter" TrueObject="{StaticResource Purple}" FalseObject="{StaticResource FadedGreen}" />
        <converters:TypedBoolConverters x:Key="SecondConverter" TrueObject="{StaticResource White}" FalseObject="{StaticResource Purple}" />
    </ContentPage.Resources>

This, however needs you to create a Resource for each parameter pair. But it would be a workaround for your issue atm.

Awesomer9561 commented 2 years ago

You can define the Paramters in here.

 <ContentPage.Resources>
        <converters:TypedBoolConverters x:Key="FirstConverter" TrueObject="{StaticResource Purple}" FalseObject="{StaticResource FadedGreen}" />
        <converters:TypedBoolConverters x:Key="SecondConverter" TrueObject="{StaticResource White}" FalseObject="{StaticResource Purple}" />
    </ContentPage.Resources>

This, however needs you to create a Resource for each parameter pair. But it would be a workaround for your issue atm.

But I want to bind properties from ViewModel not StaticResources

jfversluis commented 2 years ago

@StephaneDelcroix I think the parameter never supported data binding and one should use a MultiBinding correct?

mrnams commented 9 months ago

This is hell, wasted my two day without solution, cant bind with data and value converter, basically i hate android , who the hell developed this pathetic OS who is resource hungry, Microsoft could have invested in making windows apps for windows phone than supporting development of this pathetic android OS