none
Context menu on a ListView with no selected item RRS feed

  • Question

  • Hello,
    I have a context menu in a ListView. The context menu contains 3 items and each item is linked to a command. The commands contains CanExecute entries to specifiy which items must be enable in the menu, depending on the current selection. The menu contains an "Insert" item to insert a new entry in the list.
    The problem is that, when no entry is selected in the ListView (the list is empty or no item selected), WPF does not call the CanExecute method and always disables the menu.
    Is there anything I can do in XAML to have the menu working correctly in this case?

    Here is the ListView definition:

    Code Snippet

          <ListView Name="lst_Account" MouseDoubleClick="DoEditAccount">
            <ListView.CommandBindings>
              <CommandBinding Command="{x:Static cmd:CCommands.New}"    CanExecute="IsAccountingOpen"  Executed="DoNewAccount" />
              <CommandBinding Command="{x:Static cmd:CCommands.Edit}"   CanExecute="IsAccountSelected" Executed="DoEditAccount" />
              <CommandBinding Command="{x:Static cmd:CCommands.Delete}" CanExecute="IsAccountSelected" Executed="DoDeleteAccount" />
            </ListView.CommandBindings>
            <ListView.ContextMenu>
              <ContextMenu>
                <MenuItem Command="{x:Static cmd:CCommands.AccountNew}"  Header="_Nouveau..." InputGestureText="Insert" ToolTip="Crée un nouveau compte" />
                <MenuItem Command="{x:Static cmd:CCommands.AccountEdit}" Header="_Modifier"   InputGestureText="Enter"  ToolTip="Modifie le compte" />
                <MenuItem Command="{x:Static cmd:CCommands.AccountDel}"  Header="_Effacer..." InputGestureText="Delete" ToolTip="Supprime le compte" />
              </ContextMenu>
            </ListView.ContextMenu>
            <ListView.ItemContainerStyle>
              <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
              </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
              <os:CGridView>
                <GridViewColumn Width="159" DisplayMemberBinding="{Binding Name}">
                  <GridViewColumn.Header>
                    <GridViewColumnHeader Name="hdr_AccName" Content="Compte" HorizontalContentAlignment="Left" Click="SortAccountsByName"/>
                  </GridViewColumn.Header>
                </GridViewColumn>
                <GridViewColumn Width="80">
                  <GridViewColumn.Header>
                    <GridViewColumnHeader Name="hdr_AccBalance" Content="Solde" HorizontalContentAlignment="Right" Click="SortAccountsByBalance"/>
                  </GridViewColumn.Header>
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <TextBlock Text="{Binding BalanceStr}" TextAlignment="Right"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
              </os:CGridView>
            </ListView.View>
          </ListView>


    Thursday, February 28, 2008 6:18 AM

Answers

  • Command route (for Execute or CanExecute) depend on the current focus. Where is the focus when you right click just before ContextMenu popup?

    I suggest you to have a PreviewMouseRightButtonDown handler on ListView where you set focus on ListView in case the event source is not a ListViewItem.

     

    Thursday, February 28, 2008 9:19 AM

All replies

  • Command route (for Execute or CanExecute) depend on the current focus. Where is the focus when you right click just before ContextMenu popup?

    I suggest you to have a PreviewMouseRightButtonDown handler on ListView where you set focus on ListView in case the event source is not a ListViewItem.

     

    Thursday, February 28, 2008 9:19 AM
  • Thank Atanas,
    The problem is that the focus is not on the ListView.
    What is strange is that a right-click on an item sets the focus on the ListView, but a right-click on an empty space doesn't.
    Patrick
    Thursday, February 28, 2008 2:57 PM