Bind ContextAction command from MenuItem to ViewModel

LucasSossaiLucasSossai Member ✭✭✭

Greetings everyone,
I'm not able to pass data from my ListView Item ContextAction, I'm setting the XAML parameters right? Here is the code:

XAML:~~~~

                            <ViewCell.ContextActions>
                                <MenuItem
                                    Command="{Binding SharePlaceCommand}"
                                    CommandParameter="{Binding .}"
                                    Text="Compartilhar" />
                                <MenuItem
                                    Command="{Binding EditPlaceCommand}"
                                    CommandParameter="{Binding .}"
                                    Text="Editar" />
                                <MenuItem
                                    Command="{Binding DeletePlaceCommand}"
                                    CommandParameter="{Binding .}"
                                    IsDestructive="True"
                                    Text="Deletar" />
                            </ViewCell.ContextActions>

In VM:

    private ObservableCollection<PlacesBD> _placesList;
    public ObservableCollection<PlacesBD> PlacesList { get { return _placesList; } set { _placesList = value; OnPropertyChanged("PlacesList"); } }

    public ICommand SharePlaceCommand { get; set; }
    public ICommand EditPlaceCommand { get; set; }

    this.SharePlaceCommand = new Command<PlacesBD>(SharePlace);
    this.EditPlaceCommand = new Command<PlacesBD>(EditPlace);

    private async void SharePlace(PlacesBD place)
    {
        Debug.WriteLine(@"      SharePlace  {0}",place);
    }

    private async void EditPlace(PlacesBD place)
    {
        Debug.WriteLine(@"      EditPlace   {0}", place);
    }

Best Answer

  • LucasSossaiLucasSossai ✭✭✭
    Accepted Answer

    Solved it:

    Just changed the XAML to :

    <ListView
                        x:Name="MyListView"
                        ios:ListView.SeparatorStyle="FullWidth"
                        CachingStrategy="RecycleElement"
                        IsGroupingEnabled="false"
                        ItemsSource="{Binding PlacesList}"
                        RowHeight="60">
                        <ListView.Behaviors>
                            <local1:ListViewSelectedItemBehavior Command="{Binding PlaceTappedCommand}" Converter="{StaticResource SelectedItemConverter}" />
                        </ListView.Behaviors>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.ContextActions>
                                        <MenuItem
                                            Command="{Binding Path=BindingContext.SharePlaceCommand, Source={x:Reference Name=MyListView}}"
                                            CommandParameter="{Binding .}"
                                            Text="Compartilhar" />
                                        <MenuItem
                                            Command="{Binding Path=BindingContext.EditPlaceCommand, Source={x:Reference Name=MyListView}}"
                                            CommandParameter="{Binding .}"
                                            Text="Editar" />
                                    </ViewCell.ContextActions>
                                    <ViewCell.View>
                                        <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="0.2*" />
                                                    <ColumnDefinition Width="1*" />
                                                    <ColumnDefinition Width="1*" />
                                                </Grid.ColumnDefinitions>
                                                <Image
                                                    Grid.Column="0"
                                                    Aspect="AspectFill"
                                                    HeightRequest="50"
                                                    Source="{Binding ImageResourceId}"
                                                    VerticalOptions="Center" />
                                                <Label
                                                    Grid.Column="1"
                                                    FontAttributes="Bold"
                                                    FontSize="Large"
                                                    Text="{Binding name}"
                                                    VerticalTextAlignment="Center" />
                                                <Label
                                                    Grid.Column="2"
                                                    Margin="5,0,25,0"
                                                    FontAttributes="Bold"
                                                    FontSize="Large"
                                                    HorizontalOptions="EndAndExpand"
                                                    Text="A°C B%"
                                                    VerticalTextAlignment="Center" />
                                            </Grid>
                                        </StackLayout>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
    

Answers

  • LucasSossaiLucasSossai Member ✭✭✭
    Accepted Answer

    Solved it:

    Just changed the XAML to :

    <ListView
                        x:Name="MyListView"
                        ios:ListView.SeparatorStyle="FullWidth"
                        CachingStrategy="RecycleElement"
                        IsGroupingEnabled="false"
                        ItemsSource="{Binding PlacesList}"
                        RowHeight="60">
                        <ListView.Behaviors>
                            <local1:ListViewSelectedItemBehavior Command="{Binding PlaceTappedCommand}" Converter="{StaticResource SelectedItemConverter}" />
                        </ListView.Behaviors>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.ContextActions>
                                        <MenuItem
                                            Command="{Binding Path=BindingContext.SharePlaceCommand, Source={x:Reference Name=MyListView}}"
                                            CommandParameter="{Binding .}"
                                            Text="Compartilhar" />
                                        <MenuItem
                                            Command="{Binding Path=BindingContext.EditPlaceCommand, Source={x:Reference Name=MyListView}}"
                                            CommandParameter="{Binding .}"
                                            Text="Editar" />
                                    </ViewCell.ContextActions>
                                    <ViewCell.View>
                                        <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="0.2*" />
                                                    <ColumnDefinition Width="1*" />
                                                    <ColumnDefinition Width="1*" />
                                                </Grid.ColumnDefinitions>
                                                <Image
                                                    Grid.Column="0"
                                                    Aspect="AspectFill"
                                                    HeightRequest="50"
                                                    Source="{Binding ImageResourceId}"
                                                    VerticalOptions="Center" />
                                                <Label
                                                    Grid.Column="1"
                                                    FontAttributes="Bold"
                                                    FontSize="Large"
                                                    Text="{Binding name}"
                                                    VerticalTextAlignment="Center" />
                                                <Label
                                                    Grid.Column="2"
                                                    Margin="5,0,25,0"
                                                    FontAttributes="Bold"
                                                    FontSize="Large"
                                                    HorizontalOptions="EndAndExpand"
                                                    Text="A°C B%"
                                                    VerticalTextAlignment="Center" />
                                            </Grid>
                                        </StackLayout>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
    
Sign In or Register to comment.