Remove ContextAction MenuItem in code behind

gogcamgogcam CHMember ✭✭
edited August 2016 in Xamarin.Forms

Hello

I've the following XAML:

<ListView x:Name="lvRapporte" ItemTapped="OnRapportItemTapped">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <Grid x:Name="grid" RowSpacing="0" ColumnSpacing="0">
              <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>

              <Label Text="{Binding AUFTR_NR}" Grid.Row="0" Grid.Column="0" />
              <Label Text="{Binding PRJDESCR}" Grid.Row="0" Grid.Column="1" />
              <Label VerticalOptions="StartAndExpand" VerticalTextAlignment="Start" Margin="0,0,0,0" Text="{Binding DATUM, StringFormat='{0:dd.MM.yyyy}'}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
            </Grid>

            <ViewCell.ContextActions>
              <MenuItem Clicked="OnRapportDelete" CommandParameter="{Binding .}" Text="{i18n:Translate Loeschen}" IsDestructive="True"></MenuItem>
              <MenuItem Clicked="OnRapportCommit" CommandParameter="{Binding .}" Text="{i18n:Translate Abgeben}" IsDestructive="False" x:Name="abgegebenMenuItem"></MenuItem>
            </ViewCell.ContextActions>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

I would like to remove menu items other some conditions in code behind. How is this possible? I've found some other threads but no one lead me to the solution. Can this be so difficult?
(In my case it's not even necessary to check for which item the context menus are shown, the context menu can be for all items the same).

Thank you & Best Regards

Answers

  • PubuduGayanPubuduGayan NZMember ✭✭✭

    @gogcam If I understood correctly , why you can’t create a menu item array in your viewmodel and passed to the view based on your logic. Furthermore if you want to remove from view code behind yes you can do it either .

  • siphomaribosiphomaribo USMember ✭✭

    @gogcam said:
    Hello

    I've the following XAML:

    <ListView x:Name="lvRapporte" ItemTapped="OnRapportItemTapped">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <Grid x:Name="grid" RowSpacing="0" ColumnSpacing="0">
                  <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>
    
                  <Label Text="{Binding AUFTR_NR}" Grid.Row="0" Grid.Column="0" />
                  <Label Text="{Binding PRJDESCR}" Grid.Row="0" Grid.Column="1" />
                  <Label VerticalOptions="StartAndExpand" VerticalTextAlignment="Start" Margin="0,0,0,0" Text="{Binding DATUM, StringFormat='{0:dd.MM.yyyy}'}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
                </Grid>
    
                <ViewCell.ContextActions>
                  <MenuItem Clicked="OnRapportDelete" CommandParameter="{Binding .}" Text="{i18n:Translate Loeschen}" IsDestructive="True"></MenuItem>
                  <MenuItem Clicked="OnRapportCommit" CommandParameter="{Binding .}" Text="{i18n:Translate Abgeben}" IsDestructive="False" x:Name="abgegebenMenuItem"></MenuItem>
                </ViewCell.ContextActions>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    

    I would like to remove menu items other some conditions in code behind. How is this possible? I've found some other threads but no one lead me to the solution. Can this be so difficult?
    (In my case it's not even necessary to check for which item the context menus are shown, the context menu can be for all items the same).

    Thank you & Best Regards

  • siphomaribosiphomaribo USMember ✭✭

    I was wondering if you found the solution.... but under the ViewCell tab put the following code

    <ViewCell.ContextActions>
                            <MenuItem
                                x:Name="ItemToRemove"
                                IsDestructive="True"
                                            Clicked="RemoveItem_Clicked"
                                            CommandParameter="{Binding .}"
                                            Text="Delete" ></MenuItem>
                        </ViewCell.ContextActions>
    
Sign In or Register to comment.