Both binding source on Listview

theocangtheocang Member ✭✭✭

I have this listview, but for the MenuItem I pass a comand ToRemovePersonand ( in my VM ) but there isn't binding because there is into the listview.
how I can specify the source of the command.

<ListView ItemsSource="{Binding DatasModels}" x:Name="listViewDatas"
                          IsPullToRefreshEnabled="true"
                      RefreshCommand="{Binding RefreshCommand}"
                        IsRefreshing="{Binding IsRefreshing}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.ContextActions>
                                <MenuItem  Command="{Binding ToRemovePerson}" CommandParameter="{Binding .}"
                                Text="Delete" IsDestructive="True"/>
                            </ViewCell.ContextActions>

                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding Firstname}"/>
                                <Label Text="{Binding Lastname}"/>
                                <Label Text="{Binding Company}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.Behaviors>
                    <behaviors:ListViewBehavior Command="{Binding SelectedItemList}" Converter="{StaticResource SelectedItemConverter}" />
                </ListView.Behaviors>
            </ListView>

Best Answers

  • NMackayNMackay GB mod
    Accepted Answer

    Use RelativeSource binding to access the command (assuming it in page binding context

    Use the x:name attribute for your page

    x:Name="MyPage"
    
    <ListView ItemsSource="{Binding DatasModels}" x:Name="listViewDatas"
                              IsPullToRefreshEnabled="true"
                          RefreshCommand="{Binding RefreshCommand}"
                            IsRefreshing="{Binding IsRefreshing}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.ContextActions>
                                    <MenuItem  Command="{Binding Source={x:Reference MyPage}, Path=BindingContext.ToRemovePerson}" CommandParameter="{Binding .}"
                                    Text="Delete" IsDestructive="True"/>
                                </ViewCell.ContextActions>
    
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding Firstname}"/>
                                    <Label Text="{Binding Lastname}"/>
                                    <Label Text="{Binding Company}"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.Behaviors>
                        <behaviors:ListViewBehavior Command="{Binding SelectedItemList}" Converter="{StaticResource SelectedItemConverter}" />
                    </ListView.Behaviors>
                </ListView>
    

    Read up on Binding Paths
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path

  • theocangtheocang ✭✭✭
    Accepted Answer

    @NMackay Ok, thanks you very much.

Answers

  • NMackayNMackay GBInsider, University mod
    Accepted Answer

    Use RelativeSource binding to access the command (assuming it in page binding context

    Use the x:name attribute for your page

    x:Name="MyPage"
    
    <ListView ItemsSource="{Binding DatasModels}" x:Name="listViewDatas"
                              IsPullToRefreshEnabled="true"
                          RefreshCommand="{Binding RefreshCommand}"
                            IsRefreshing="{Binding IsRefreshing}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.ContextActions>
                                    <MenuItem  Command="{Binding Source={x:Reference MyPage}, Path=BindingContext.ToRemovePerson}" CommandParameter="{Binding .}"
                                    Text="Delete" IsDestructive="True"/>
                                </ViewCell.ContextActions>
    
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding Firstname}"/>
                                    <Label Text="{Binding Lastname}"/>
                                    <Label Text="{Binding Company}"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.Behaviors>
                        <behaviors:ListViewBehavior Command="{Binding SelectedItemList}" Converter="{StaticResource SelectedItemConverter}" />
                    </ListView.Behaviors>
                </ListView>
    

    Read up on Binding Paths
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path

  • theocangtheocang Member ✭✭✭
    Accepted Answer

    @NMackay Ok, thanks you very much.

Sign In or Register to comment.