gpt4 book ai didi

c# - "No target found for method"由 Caliburn Message.Attach() 抛出

转载 作者:太空狗 更新时间:2023-10-29 20:05:25 26 4
gpt4 key购买 nike

我有一个列表框,我正在为其设置 ItemContainer 的样式以包含上下文菜单。这是相同的 xaml。

<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
...
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>

我已经在 ViewModel 中编写了目标方法,如下所示。

public void DeleteGroup() { //ToDo
...
}

ViewModel 被设置为其中有 ListBox 的 UserControl 的 DataContext。

以上代码导致“找不到方法的目标”。我不确定为什么这不起作用。我还尝试了以下变体

<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"
cal:Action.Target="{Binding ElementName=UCRelayDispositionView, Path=DataContext}">

其中 UCRelayDispositionView 是 UserControl 的名称。

为什么上面的代码不起作用?

编辑:1还尝试了以下

<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"
cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView, Path=DataContext}">

还有这个

<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"
cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView}">

编辑:2我曾尝试在 ItemContainer 上以下列方式使用标签,但它也不起作用。

<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Tag" Value="{Binding Path=DataContext, ElementName=UCRelayDispositionView}"/>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove Group"
cal:Message.Attach="DeleteGroup()"
cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"/>
</ContextMenu>
</Setter.Value>
</Style>
</ListBox.ItemContainerStyle>

编辑 3:绑定(bind)错误

System.Windows.Data Error: 40 : BindingExpression path error: 'PlacementTarget' property not found on 'object' ''MenuItem' (Name='')'. BindingExpression:Path=PlacementTarget.Tag; DataItem='MenuItem' (Name=''); target element is 'MenuItem' (Name=''); target property is 'TargetWithoutContext' (type 'Object')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=UCRelayDispositionView'. BindingExpression:Path=DataContext; DataItem=null; target element is 'ContextMenu' (Name=''); target property is 'Tag' (type 'Object')

最佳答案

您的问题在于您试图将目标绑定(bind)到同一可视化树中不存在的元素,例如你有一个项目所在的 ContextMenu

要正确获取操作目标,您需要使用 ContextMenuPlacementTarget 属性。

查看以下有关 XAML 的 SO 的答案

WPF Context Menus in Caliburn Micro

所以下面的 XAML 应该可以工作:

<MenuItem Header="Blah" cal:Message.Attach="SomeMethod()" cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}">

这应该在 ContextMenu 上寻找 PlacementTarget 并将操作的目标设置为 PlacementTarget.Tag 的值(应该是 ListBoxItem)。

如果您将 ListBoxItem.Tag(正如您已经完成的那样)设置为父容器(ListBox)的 DataContext,您应该没问题

所以标签绑定(bind)应该是:

<Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>

例如整个事情应该是:

<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}">
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup()" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>

关于c# - "No target found for method"由 Caliburn Message.Attach() 抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com