[否目标发现方法"由卡利Message.Attach抛出() [英] "No target found for method" thrown by Caliburn Message.Attach()

查看:979
本文介绍了[否目标发现方法"由卡利Message.Attach抛出()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些,我的造型ItemContainer包括上下文菜单的列表框。这里是相同的XAML

 < ListBox.ItemContainerStyle> 
<风格的TargetType ={X:输入一个ListBoxItem}>

< setter属性=文本菜单>
< Setter.Value>
<&文本菜单GT;
<菜单项标题=删除组CAL:Message.Attach =DeleteGroup/>
< /文本菜单>
< /Setter.Value>
< /二传手>
< /样式和GT;



我已编码的视图模型对象的方法如下。

 公共无效DeleteGroup(){//待办事项

}

视图模型设置为用户控件,其中有ListBox的DataContext的。



上面的代码结果没有目标发现方法。我不知道为什么,这是行不通的。我也曾尝试以下变化

 <菜单项标题=删除组CAL:Message.Attach =DeleteGroup
CAL:Action.Target ={绑定的ElementName = UCRelayDispositionView,路径=的DataContext}>

其中UCRelayDispositionView是用户控件的名称。



为什么上面的代码不工作



修改:1
也试过以下

 <菜单项标题=删除组CAL:Message.Attach =DeleteGroup
CAL:Action.TargetWithoutContext ={结合的ElementName = UCRelayDispositionView,路径=的DataContext}>





 <菜单项标题=删除组CAL:Message.Attach =DeleteGroup
CAL:Action.TargetWithoutContext ={绑定的ElementName = UCRelayDispositionView}>



编辑:2
我曾尝试使用Tag 。在ItemContainer下面的方式,但它也不行

 < ListBox.ItemContainerStyle> 
<风格的TargetType ={X:输入一个ListBoxItem}>
< setter属性=变量VALUE ={绑定路径= DataContext的,的ElementName = UCRelayDispositionView}/>
< setter属性=文本菜单>
< Setter.Value>
<&文本菜单GT;
<菜单项标题=删除组
CAL:Message.Attach =DeleteGroup()
CAL:Action.TargetWithoutContext ={绑定路径= PlacementTarget.Tag,的RelativeSource = {自我的RelativeSource}}/>
< /文本菜单>
< /Setter.Value>
< /样式和GT;
< /ListBox.ItemContainerStyle>

修改3:装订错误



  System.Windows.Data错误:40:BindingExpression路径错误:'PlacementTarget'属性不是'对象'''菜单项(名称='')'找到。 BindingExpression:路径= PlacementTarget.Tag;的DataItem ='菜单项'(名称='');目标元素是'菜单项(名称=''); target属性是'TargetWithoutContext'(类型'对象')
System.Windows.Data错误:4:无法为参照的ElementName = UCRelayDispositionView'绑定找到源头。 BindingExpression:路径= DataContext的;的DataItem = NULL;目标元素是'文本菜单'(名称=''); target属性是'标签'(类型'对象')


解决方案

您的问题在于,你正试图目标绑定到不一样的视觉树如存在的元素你有一个文本菜单上的项目所在。



要正确地得到一个行动目标,你需要使用文本菜单取值 PlacementTarget 属性。



看看下面的回答所以对于XAML



WPF上下文菜单中卡利微



我不知道,如果你需要使用标签属性伎俩你正在使用 ItemContainerStyle ,所以你也许能逃脱:

 <菜单项标题=嗒嗒CAL:Message.Attach =的someMethod()CAL:Action.TargetWithoutContext ={绑定路径= PlacementTarget.DataContext,的RelativeSource = {自我的RelativeSource}}> 



编辑:在这里,



错误上述相关来源应该是文本菜单(这就是为什么你的PlacementTarget找不到属性绑定错误)

 <菜单项标题= 嗒嗒CAL:Message.Attach =的someMethod()CAL:Action.TargetWithoutContext ={绑定路径= PlacementTarget.Tag,的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType =文本菜单}}> 

这应该寻找PlacementTarget的文本菜单和设置操作定位到PlacementTarget对象的标签(这应该是ListBoxItem中)。如果设置了ListBoxItems标签(因为你已经完成)是父容器的DataContext的(列表框),你应该确定



所以标签结合应该是

 < setter属性=变量VALUE ={绑定路径= DataContext的,的RelativeSource = {的RelativeSource模式= FindAncestor, AncestorType =列表框}}/> 



例如。整个事情应该是:



 < ListBox.ItemContainerStyle> 
<风格的TargetType ={X:输入一个ListBoxItem}>
< setter属性=变量VALUE ={绑定路径= DataContext的,的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType =列表框}}/>
< setter属性=文本菜单>
< Setter.Value>
<文本菜单CAL:Action.TargetWithoutContext ={绑定路径= PlacementTarget.Tag,的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType =文本菜单}}>
<菜单项标题=删除组CAL:Message.Attach =DeleteGroup()/>
< /文本菜单>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /ListBox.ItemContainerStyle>


I have a list box for which I am styling ItemContainer to include a context menu. Here is the xaml for the same.

<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>

I have coded the target method in ViewModel as given below.

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

The ViewModel is set as the DataContext of the UserControl in which there is the ListBox.

The above code results in "no target found for method". I am not sure why this doesn't work. I have also tried the following variation

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

where UCRelayDispositionView is the name of the UserControl.

Why does the above code do not work?

Edit: 1 Also tried the following

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

and this

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

EDIT: 2 I have tried to use the Tag in the following way on ItemContainer but it doesn't work either.

<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>

EDIT 3: Binding Errors

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')

解决方案

Your problem lies in that you are trying to bind the target to an element which doesn't exist in the same visual tree e.g. you have a ContextMenu on which the item resides.

To correctly get an action target, you need to use the ContextMenus PlacementTarget property.

Check out the following answer on SO for the XAML

WPF Context Menus in Caliburn Micro

I'm not sure if you need to use the Tag property trick as you are using ItemContainerStyle so you might be able to get away with:

<MenuItem Header="Blah" cal:Message.Attach="SomeMethod()" cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">

Edit:

Error here, the relative source above should be the ContextMenu (that's why you are having the PlacementTarget property not found binding error)

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

This should look for the PlacementTarget on the ContextMenu and set the actions target to the Tag of the PlacementTarget object (which should be the ListBoxItem). If you set the ListBoxItems tag (as you have already done) to be the DataContext of the parent container (the ListBox) you should be ok

so the tag binding should be:

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

e.g. the whole thing should be:

<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>

这篇关于[否目标发现方法&QUOT;由卡利Message.Attach抛出()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆