Xamarin Form Add Multiple Binding

Alice 61 Reputation points
2023-01-30T07:42:11.72+00:00

Please I need help.

I have my Xamarin Form app that works very well with DataBinding to its View Model named OrderDetailViewModel.

But today I have to add another DataBinding field in line 10 Binding OrderStatusMessage that is bind to its Xaml.cs file (code behind) but out of curiosity this field doesn't work I mean it is not changing the Label Text dynamically after binding the label text to a property located in its xaml.cs file (code behind file).

In line 4 you can find the data binding to MVVM file called OrderDetailViewModel which works well.

But the issue is in line 10 Binding OrderStatusMessage which is a DataBinding to its own xaml.cs file doesn't change the Label text to new message. I tested the same code Binding OrderStatusMessage outside of the CollectionView.ItemTemplate tag and it's working properly, so only when I put inside CollectionView.ItemTemplate tag that's is not pulling the updated text.

In the below code there's only one property that is bind to its xaml.cs file (code behind) which is in line 10 {Binding OrderStatusMessage}, the rest are bind to the MVVM which works well.

PS : I would like to add Multiple Binding to this below code in line 4 :

filename : OrderDetailViewPage.xaml

<CollectionView.ItemTemplate >
                            <DataTemplate >
                                <StackLayout
                                    xct:TouchEffect.LongPressCommand="{Binding LongPressCommand, Source={RelativeSource AncestorType={x:Type local:OrderDetailViewModel}}}"
                                    xct:TouchEffect.LongPressCommandParameter="{Binding .}"
                                    xct:TouchEffect.PressedScale="1.2"
                                    xct:TouchEffect.NativeAnimation="True">

                                    <StackLayout>
                                        <Label Text="{Binding OrderStatusMessage}" HorizontalOptions="CenterAndExpand" FontFamily="fasolid" FontSize="18" FontAttributes="Bold" TextColor="Black"/>
                                    </StackLayout>

                                    <Label Text="{Binding Id}"
                                        FontSize="Medium"/>
                                    <Label Text="Order number" HorizontalOptions="CenterAndExpand"/>
                                    <Label Text="{Binding OrderId}"
                                        TextColor="Purple" FontAttributes="Bold"
                                        FontSize="Large" HorizontalOptions="CenterAndExpand"/>
                                    <Label Text="{Binding OrderDetail}"
                                        TextColor="Purple"
                                        FontSize="Small" />
                                    <Label Text="{Binding OrderDate}"
                                        TextColor="Purple"
                                        FontSize="Small" />
                                    <Label Text="{Binding IsOrderComplete}"
                                        TextColor="Purple"
                                        FontSize="Small" />
                                </StackLayout>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,272 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 33,381 Reputation points Microsoft Vendor
    2023-01-31T05:03:30.5166667+00:00

    Hello,

    I tested the same code Binding OrderStatusMessage outside of the CollectionView.ItemTemplate tag and it's working properly, so only when I put inside CollectionView.ItemTemplate tag that's is not pulling the updated text.

    This is because in the CollectionView.ItemTemplate tag, the binding context becomes the Item class you are binding in the CollectionView.

    The official workaround for this problem is given in Xamarin's documentation, which is to use relative binding to change the binding context.

    Please refer to Xamarin.Forms Relative Bindings-Bind to an ancestor to get more details.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Alice 61 Reputation points
    2023-02-09T01:33:05.5566667+00:00

    Sorry for late reply, I was inundated with tasks.

    0 comments No comments