簡體   English   中英

在上下文菜單中未調用DelegateCommand

[英]DelegateCommand is not invoked in context menu

我似乎無法使該右鍵單擊彈出菜單起作用。

<TreeView Name="NavigationPanel" ItemsSource="{Binding NavigationList}" />
<Style TargetType="{x:Type TreeViewItem}" >
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu  DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}, Path=DataContext}">
                <MenuItem Header="Delete" Command="{Binding Path=CommandPopupClick}" CommandParameter="{Binding Path=SelectedItem}"/>
                <Separator />
                <MenuItem Header="Properties" Command="{Binding Path=CommandPopupClick}" CommandParameter="{Binding Path=SelectedItem}"/>
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>
//Delegated command
public DelegateCommand CommandPopupClick { get; set; } 

//Assigning the delegate command.
CommandPopupClick = new DelegateCommand(PopupClick, CanMyCommand);

//Event for rightclick option clicked
public void PopupClick(Object Parameters)
{
    var something = Parameters; //Break is here to see if the event fires
}

我可以在彈出菜單中看到“刪除”和“屬性”項。 但是,當我單擊其中任何一個時,都不會觸發該事件。

注意:委托命令系統適用於所有其他情況,我認為這不是問題。

我真的不想使用Name.Click += new RoutedEvent()如果我可以幫助,請Name.Click += new RoutedEvent()

謝謝。

根據要求進行調試中的錯誤

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
'Enterprise.exe' (CLR v4.0.30319: Enterprise.exe): Loaded 'Microsoft.GeneratedCode'. 
'Enterprise.exe' (CLR v4.0.30319: Enterprise.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemCore\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemCore.dll'. Cannot find or open the PDB file.
A first chance exception of type 'System.IO.IOException' occurred in PresentationFramework.dll
A first chance exception of type 'System.IO.IOException' occurred in PresentationCore.dll
'Enterprise.exe' (CLR v4.0.30319: Enterprise.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Cannot find or open the PDB file.
The thread 0x1954 has exited with code 259 (0x103).

解析度:

    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                <MenuItem Header="Delete" 
                          Command="{Binding CommandPopupClick}" 
                          CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource  AncestorType=ContextMenu}}" 
                          CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

<TreeView Name="NavigationPanel" ItemsSource="{Binding NavigationList}" Tag="{Binding Path=DataContext, ElementName=Main}"/>

感謝所有協助的人。

您不能使用RelativeSource BindingContextMenu訪問TreeView.DataContext ,因為它不在主UI可視樹中。 這是一個非常有據可查的問題,解決方案是使用ContextMeny.PlacementTarget屬性和應用了Contextmenu的對象上的Tag屬性,將DataContext對象傳遞到ContextMenu

如今已經寫了很多次,現在我更願意要求您閱讀我對ContextMenu的回答。PlacementTarget未設置,不知道為什么,並且在datagrid中添加上下文菜單,如何獲取選擇項有關堆棧的完整解釋和代碼示例,請在此處對問題進行評估 如果您需要進一步的幫助,只需在Internet上搜索“ WPF ContextMenu DataContext ”或類似的內容,就可以找到許多與此確切主題相關的教程。

ContextMenu不屬於可視樹,因此FindAncestory數據綁定將無法工作。

不過,您可以在ContextMenu顯式設置DataContext

例如,如果可以的話,您可以直接在XAML中創建ViewModel / DataContext的實例。

<Style TargetType="{x:Type TreeViewItem}">
  <Setter Property="ContextMenu">
    <Setter.Value>
      <ContextMenu>
        <ContextMenu.DataContext>
          <local:MyViewModel/>
        </ContextMenu.DataContext>

        <MenuItem Command="{Binding Path=CommandPopupClick}"
                  CommandParameter="{Binding Path=SelectedItem}"
                  Header="Delete" />
        <Separator />
        <MenuItem Command="{Binding Path=CommandPopupClick}"
                  CommandParameter="{Binding Path=SelectedItem}"
                  Header="Properties" />
      </ContextMenu>
    </Setter.Value>
  </Setter>
</Style>

或者從StaticResource獲取它。

  <Window.Resources>
    <local:MyViewModel x:Key="ctx"/>

    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="ContextMenu">
        <Setter.Value>
          <ContextMenu DataContext="{StaticResource ctx}">

            <MenuItem Command="{Binding Path=CommandPopupClick}"
                      CommandParameter="{Binding Path=SelectedItem}"
                      Header="Delete" />
            ....

          </ContextMenu>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>

由於其他人正在尋找有關此問題的答案,因此越來越多地了解C#和WPF。 答案很簡單。

您的處理方式錯誤。 如果您需要一遍又一遍地使用“行為”,並且使用MVVM樣式代碼(或者您以為是因為它可能還不存在),則需要使用他們所謂的附加屬性。

簡單的說。

  1. 創建一個附加的屬性類。 http://dotnetbyexample.blogspot.com.au/2010/05/attached-dependency-properties-for.html應該可以幫助您入門
  2. 在下面的菜單項示例中創建模板
  3. 創建標准菜單項。
  4. 處理附加屬性PropertyMetadata事件處理程序中的所有內容。

      <Style TargetType="MenuItem"> <Setter Property="Properties:ControlMenuItem.InvokeClick" Value="{Binding RelativeSource={RelativeSource Self}}"/> </Style> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM