2012-11-27 41 views
6

我有一個列表框,我爲其設計ItemContainer以包含上下文菜單。這是同樣的xaml。「Caliburn Message.Attach()拋出的方法沒有找到任何目標」

<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:綁定錯誤

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

回答

10

你的問題在於,你試圖結合靶點到不一樣的視覺樹存在例如一個元素你有一個項目所在的ContextMenu

要正確獲取操作目標,您需要使用ContextMenuPlacementTarget屬性。

看看下面的答案上所以對於XAML

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

這應該尋找PlacementTargetContextMenu和設置目標採取行動的值爲PlacementTarget.Tag(應該是ListBoxItem)。

如果設置ListBoxItem.Tag(因爲你已經這樣做)是父容器的DataContext(在ListBox)你應該確定

所以標籤綁定應該是:

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

您的代碼不起作用。仍然給出相同的錯誤。就使用標籤而言,我在哪裏放置它?我在ItemContainerStyle上有這個上下文菜單! – Jatin

+0

如果你看看我發佈的SO鏈接,有一個例子可以說明。基本上你必須使用'ContextMenu.PlacementTarget'屬性來獲取產生上下文菜單的項並綁定到它的'DataContext',儘管它可能需要你將父容器的'DataContext'轉換爲'Tag'屬性。這對我很有用(可能值得在視圖中創建一個事件,這樣您可以在單擊菜單項時進入代碼隱藏和調試,這樣您可以瀏覽對象圖並發現正確的綁定路徑) – Charleh

+0

我編輯了標記爲** EDIT 2 **的問題。我不確定我們是否可以使用Style來設置標籤,但是仍然存在相同的錯誤。 – Jatin

相關問題