Dennis Schröer
Dennis Schröer

Reputation: 2412

WPF - Command not responding

I have the following code in which DeleteBelegkopfCommand is not responding when clicking the MenuItem:

[...]
<HierarchicalDataTemplate.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Margin="2">
            <Image Margin="0,0,2,2"  
                   Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}},
                                    Path=Header, Converter={x:Static conv:HeaderToImageConverter.Instance}}"/>
                 <TextBlock Text="{Binding BelegtypDesc, StringFormat='Beleg: {0}'}"
                            acb:LeftClickCommands.MouseEventParameter="{Binding}" 
                            acb:LeftClickCommands.MouseLeftButtonClickCommand="{Binding ElementName=CaseTreeView, Path=DataContext.ChangeSelectionCommand}">
                 </TextBlock>
            <StackPanel.ContextMenu>                            
                <ContextMenu>
                    <MenuItem Header="Beleg löschen"
                              Command="{Binding Path=PlacementTarget.Tag.DataContext.DeleteBelegkopfCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"
                              CommandParameter="{Binding Path=CurrentBelegkopf.Id}">
                    </MenuItem>
                </ContextMenu>
            </StackPanel.ContextMenu>
        </StackPanel>
    </DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>

This is where I define the Command. CanDeleteBelegkopf is always true for testing:

static DelegateCommand<Guid?> _deleteBelegkopfCommand;

public DelegateCommand<Guid?> DeleteBelegkopfCommand
{
    get
    {
        if (_deleteBelegkopfCommand == null)
            _deleteBelegkopfCommand = new DelegateCommand<Guid?>(OnDeleteBelegkopf, CanDeleteBelegkopf);
        return _deleteBelegkopfCommand;
    }
}

Does anyone have an idea why my command is not responding at all? I tested it with other commands which are working at other places in my code - it didn't work either. So I think it's something about my Binding with the command.

Upvotes: 1

Views: 81

Answers (1)

Ahmad
Ahmad

Reputation: 1524

ContextMenus are separate controls with their own VisualTree:

If you look in the output window, i am guessing you see binding exceptions. If so, then have a look at :

http://www.wpftutorial.net/RoutedCommandsInContextMenu.html

Upvotes: 1

Related Questions