【问题标题】:Menu click not firing its bound command菜单单击未触发其绑定命令
【发布时间】:2020-10-30 16:31:23
【问题描述】:

我从这个视图开始:

<Window x:Class="MyApp.UI.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyApp.UI.Views"
        mc:Ignorable="d"
        Title="MyApp" Height="300" Width="500" WindowStartupLocation="CenterScreen">
  <Grid>
    <Button
      HorizontalAlignment="Left"
      VerticalAlignment="Top"
      Command="{Binding ShutdownCommand}"
      Content="_Exit" 
      Margin="390,225,0,0"
      Width="75"/>
  </Grid>
</Window>

代码隐藏

Imports MyApp.UI.ViewModels

Namespace MyApp.UI.Views
  Public Class MainView
    Public Sub New(ViewModel As IMainViewModel)
      Me.InitializeComponent()

      Me.ViewModel = ViewModel
      Me.DataContext = Me.ViewModel
    End Sub

    Private Sub MainView_Loaded(Sender As MainView, e As RoutedEventArgs) Handles Me.Loaded
      Me.ViewModel.Load
    End Sub

    Private ReadOnly ViewModel As MainViewModel
  End Class
End Namespace

视图模型

Imports MyApp.UI.Commands

Namespace MyApp.UI.ViewModels
  Public Class MainViewModel
    Implements IMainViewModel

    Public Sub New()
      Me.ShutdownCommand = New DelegateCommand(AddressOf Me.Shutdown)
    End Sub

    Public Sub Shutdown() Implements IMainViewModel.Shutdown
      Application.Current.Shutdown()
    End Sub

    Public Sub Load() Implements IMainViewModel.Load
    End Sub

    Public ReadOnly Property ShutdownCommand As ICommand
  End Class
End Namespace

委托命令

Imports Intexx

Namespace MyApp.UI.Commands
  Public Class DelegateCommand
    Implements ICommand

    Public Sub New(Execute As Action(Of Object))
      Me.New(Execute, Nothing)
    End Sub

    Public Sub New(Execute As Action(Of Object), CanExecute As Func(Of Object, Boolean))
      If Execute.IsNothing Then
        Throw New ArgumentNullException(NameOf(Execute))
      Else
        Me._CanExecute = CanExecute
        Me._Execute = Execute
      End If
    End Sub

    Public Function CanExecute(Parameter As Object) As Boolean Implements ICommand.CanExecute
      Return Me._CanExecute.IsNothing OrElse Me._CanExecute(Parameter)
    End Function

    Public Sub Execute(Parameter As Object) Implements ICommand.Execute
      Me._Execute(Parameter)
    End Sub

    Public Sub RaiseCanExecuteChanged()
      RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
    End Sub

    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

    Private ReadOnly _CanExecute As Func(Of Object, Boolean)
    Private ReadOnly _Execute As Action(Of Object)
  End Class
End Namespace

单击Exit 按钮按预期工作。应用程序退出。

然后我添加了一个菜单:

<Window x:Class="MyApp.UI.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyApp.UI.Views"
        mc:Ignorable="d"
        Title="MyApp" Height="300" Width="500" WindowStartupLocation="CenterScreen">
  <Grid>
    <Menu>
      <MenuItem Header="_File">
        <MenuItem
          Header="E_xit"
          Command="{Binding ShutDownCommand}"/>
      </MenuItem>
      <MenuItem Header="_Help">
        <MenuItem Header="_About MyApp"/>
      </MenuItem>
    </Menu>
    <Button
      HorizontalAlignment="Left"
      VerticalAlignment="Top"
      Command="{Binding ShutdownCommand}"
      Content="_Exit" 
      Margin="390,225,0,0"
      Width="75"/>
  </Grid>
</Window>

单击Exit 按钮仍然有效,但单击File=&gt;Exit 不会产生任何结果。我没有更改代码隐藏或 ViewModel 中的任何内容。

This one 是相关的,但并不完全相同。我的MenuItem 绑定命令是孩子,而不是父母。

我在这里缺少什么?为什么File=&gt;Exit点击不调用绑定命令?

【问题讨论】:

    标签: c# wpf vb.net data-binding menu


    【解决方案1】:

    好的,知道了。

    对 WPF 有点陌生,我忘记了命令绑定(除其他外)是区分大小写的。

    当我将MenuItem 的命令绑定从ShutDownCommand 更改为ShutdownCommand 时,它可以工作。应用程序按预期退出。

    如果 IntelliSense 能在这里工作就好了。

    【讨论】:

    • IntelliSense 在 DataContext 已知时工作:例如。来自 Xaml 的 DataContext 集或已知类型的模板。在您的情况下,您应该在调试时在输出窗口中查找绑定错误。
    • look for binding errors in the output window when debugging 我喜欢这个主意。不幸的是,输出窗口中什么也没有出现——至少对我来说是这样。 here 的任何建议都没有任何效果。
    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 2021-04-14
    • 2012-01-15
    • 2017-04-09
    • 2014-02-16
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多