Binding question (newbie)

-- -- 957 Reputation points
2021-06-27T20:34:58.36+00:00

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>  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alessandro Caliaro 4,196 Reputation points
    2021-06-28T06:31:53.433+00:00

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

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,416 Reputation points Microsoft External Staff
    2021-06-29T02:04:17.18+00:00

    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://learn.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.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.