【问题标题】:WPF - Command not respondingWPF - 命令没有响应
【发布时间】:2015-12-07 11:23:10
【问题描述】:

我有以下代码,其中 DeleteBelegkopfCommand 在单击 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>

这是我定义命令的地方。 CanDeleteBelegkopf 始终适用于测试:

static DelegateCommand<Guid?> _deleteBelegkopfCommand;

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

有人知道为什么我的命令根本没有响应吗?我用在我的代码的其他地方工作的其他命令对其进行了测试——它也没有工作。所以我认为这与我与命令的绑定有关。

【问题讨论】:

    标签: wpf xaml binding command


    【解决方案1】:

    ContextMenus 是具有自己的 VisualTree 的独立控件:

    如果您查看输出窗口,我猜您会看到绑定异常。如果是这样,那么看看:

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

    【讨论】:

    • 感谢您的回答。我已经按照 wpftutorial 中的说明手动绑定了 CommandTarget:RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu} 正如教程中所写,它应该像这样工作。不幸的是,它不起作用。我错过了什么吗?
    • 查看 Visual Studio 输出窗口,查看单击菜单项时是否引发任何绑定异常。我怀疑 Binding Path=PlacementTarget.Tag.DataContext.DeleteBelegkopfCommand
    • 当我在输出中使用 CTRL+F 搜索时,没有任何例外,也没有任何关于 DeleteBelefkopfCommand 的文本。 :( 当我单击 MenuItem 时,输出中绝对没有任何反应。
    • 我建议您现在通过删除 CommandParameter 来简化它。而是添加一个 CommandTarget,如 wpf.2000things.com/2014/06/19/… 所示,并将上下文菜单添加为其目标。然后将命令更改为 Command="{Binding DeleteBelegkopfCommand} ... CommandTarget 应将命令数据上下文设置为 ContextMenu 的数据上下文。
    • 这是个好主意,但没有帮助。现在我的绑定出现异常:BindingExpression 路径错误:在“对象”“Belegkopf”(HashCode=46816480)上找不到“DeleteBelegkopfCommand”属性。 BindingExpression:Path=DeleteBelegkopfCommand; DataItem='Belegkopf' (HashCode=46816480);目标元素是'MenuItem'(名称='');目标属性是“Command”(输入“ICommand”)
    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2022-01-04
    相关资源
    最近更新 更多