question

78669366 avatar image
0 Votes"
78669366 asked WenyanZhang-MSFT edited

Binding question (newbie)

Hi

My UpcomingJobsPage is bound to UpcomingJobsViewModel and has the xaml as below at the end.

The RefreshView is bound to UpcomingJobsViewModel and calls command LoadDataCommand to load data into the ObservableCollection UpcomingJobsList. The listview is bound to UpcomingJobsList. Pretty standard stuff.

My question is how in general can I choose to bind my UpcomingJobsPage view elements to either UpcomingJobsViewModel properties or UpcomingJobsList properties? For instance in below code I need the 'From' to be coming from UpcomingJobsList but it says;

109665-hs-xamarin-binding.png

Thanks

Regards


  <ContentPage.Content>
         <Grid>
             <RefreshView x:DataType="viewmodels:UpcomingJobsViewModel" Command="{Binding LoadDataCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
                 <ListView x:Name="listView" ItemsSource="{Binding UpcomingJobsList}" >
                     <ListView.ItemTemplate>
                         <DataTemplate >
                             <ViewCell>
                                 <ViewCell.View>
                                     <Grid >
                                         <Label Text="{Binding From}" />
                                     </Grid>
                                 </ViewCell.View>
                             </ViewCell>
                         </DataTemplate>
                     </ListView.ItemTemplate>
                 </ListView>
             </RefreshView>
         </Grid>
     </ContentPage.Content>



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

alessandrocaliaro avatar image
2 Votes"
alessandrocaliaro answered

"From" is a member of UpcomingJobsList model, so you should set the ListView "x:DataType" to UpcomingJobsList model.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WenyanZhang-MSFT avatar image
1 Vote"
WenyanZhang-MSFT answered WenyanZhang-MSFT edited

Hello,
Welcome to our Microsoft Q&A platform!

As alessandrocaliaro said, you could set the ListView "x:DataType" to UpcomingJobsList model, like the code following:

 <DataTemplate x:DataType=" the model you used in UpcomingJobsList">

For more information, you can refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/compiled-bindings

Best Regards,

Wenyan Zhang




If the response is helpful, please click "Accept Answer" and upvote it.

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.