How do I properly bind to a label's span?

MrSpoofMrSpoof USMember ✭✭
edited August 2018 in Xamarin.Forms

I have a label that looks something like this:

<Label x:Name="MovAvgLabel" Grid.Column="0" HorizontalOptions="Center" HorizontalTextAlignment="Center" >
                            <Label.FormattedText>
                                <FormattedString>
                                    <Span Text="{Binding Path=MovingAverage}" FontSize="Medium" FontAttributes="Bold"/>
                                    <Span Text="&#10;Moving Average&#10;Today"/>
                                </FormattedString>
                            </Label.FormattedText>
                        </Label>

The above code fails to bind to my ViewModel. I don't think it's the binding context, I tried binding to a regular label like so:

<Label Grid.Column="0" HorizontalOptions="Center" HorizontalTextAlignment="Center" Text="{Binding MovingAverage}"/>

And that seemed to work fine. What am I doing wrong here? The two examples shared the same grid parent. What's weird is that I've bound objects to spans before and it worked fine, although it was in the context of a custom ViewCell so maybe there's some weirdness there.

Best Answer

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited August 2018

    Test your code but failed to reproduce this issue, here is my code:

    <StackLayout>
            <!-- Place new controls here -->
            <Label x:Name="MovAvgLabel" HorizontalOptions="Center" HorizontalTextAlignment="Center" >
                <Label.BindingContext>
                    <local:MyViewModel />
                </Label.BindingContext>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="{Binding Path=MovingAverage}" FontSize="Medium" FontAttributes="Bold"/>
                        <Span Text="&#10;Moving Average&#10;Today"/>
                    </FormattedString>
                </Label.FormattedText>
            </Label>
            <Label Text="Second label"/>
            <Label Text="{Binding MovingAverage}">
                <Label.BindingContext>
                    <local:MyViewModel />
                </Label.BindingContext>
            </Label>
        </StackLayout>
    

    Effect:

    Could you please share a more detailed information so that we can reproduce this issue on our side?

  • MrSpoofMrSpoof USMember ✭✭

    Hmm, interesting. I notice that you're setting your binding context in XAML. My context is slightly different, I'm using Prism to auto-bind my ViewModels to my views. It does seem to be working properly, I can inspect the binding context at runtime and I see that it's been set. Also, other properties DO bind to the ViewModel correctly.

    Now, that being said, I think I've narrowed down the problem. I tried setting an initial value to my property, and when I ran my app that initial property appeared. When I placed those two labels side by side, the label with the regular Text property binding received the updated, and the one with the formatted string property did not. So I think there's a problem with the formatted text not listening to INotifyPropertyChanged events.

    Could you try having your pages OnAppearing event cause your VM to update its properties and see if you get the same problem?

  • SirJohnKSirJohnK USMember

    Worked great! Thanks! :)

  • PrullebakPrullebak NLMember ✭✭

    Thanks worked!

Sign In or Register to comment.