Search This Blog

Tuesday 10 November 2020

How to Use Relative Source in ListView

TestPage.xaml

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:TestApp.ViewModels.TestPageViewModel" 
             x:Class="TestApp.Views.TestPage">
    <ContentPage.Content>
        <StackLayout >
            <ListView  Margin="10,-10,0,0"  HasUnevenRows="True"  SeparatorVisibility="None" ItemsSource="{Binding EmployeeDetails}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Margin="33,0,0,0" Text="{Binding Title}"  TextColor="Gray" />
                                <Label  Margin="33,0,0,0"  TextColor="Gray">
                                    <Label.FormattedText >
                                        <FormattedString  >
                                            <Span Text="{Binding FirstName}" />
                                            <Span Text=" to " />
                                            <Span Text="{Binding LastName}" />
                                        </FormattedString>
                                    </Label.FormattedText>
                                </Label>
                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer CommandParameter="{Binding .}" Command="{Binding Source={RelativeSource AncestorType={x:Type local:TestPageViewModel}},Path=TestCommand}" />
                                </StackLayout.GestureRecognizers>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.Footer>
                    <Label HeightRequest="80" />
                </ListView.Footer>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

1 comment:

Popular Posts