我正在尝试在 XAML 中绑定 RelayCommand 的命令参数<MessageTokens> 如下,其中 MessageTokens 是一个枚举

 public ICommand OpenInvoiceWindowCommand
        {
            get
            {
                return _openInvoiceWindowCommand ??
                       (_openInvoiceWindowCommand = new RelayCommand<MessageTokens>(OpenInvoiceWindow, prm => CanExecuteOpenInvoiceWindowCommand));
            }
        }

XAML 是:

  <Controls1:RadMenuItem Command="{Binding SupplierInvoiceViewModel.OpenInvoiceWindowCommand}" CommandParameter="{Binding Source={x:Type ShipmentDetails:DebitInvoiceViewModelBase}, Path=MessageTokens.SupplierInvoice}" Header="Register a supplier's invoice"/>

使用 Telerik 的 RadGridView 代码崩溃

   at GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecute(Object parameter)
   at Telerik.Windows.Controls.RadMenuItem.CanExecuteApply() in c:\Builds\WPF_Scrum\HotFix_2010_Q3\Sources\Development\Controls\Navigation\Menu\RadMenuItem.cs:line 2105
   at Telerik.Windows.Controls.RadMenuItem.ChangeCommand(ICommand oldCommand, ICommand newCommand) in c:\Builds\WPF_Scrum\HotFix_2010_Q3\Sources\Development\Controls\Navigation\Menu\RadMenuItem.cs:line 2124
   at Telerik.Windows.Controls.RadMenuItem.OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in c:\Builds\WPF_Scrum\HotFix_2010_Q3\Sources\Development\Controls\Navigation\Menu\RadMenuItem.cs:line 1623
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)

ETC。

如果我将参数更改为 RelayCommand<string> 代替 RelayCommand<MessageTokens> 代码运行良好。

我不愿意这样做,因为我失去了强输入参数的所有好处。

我怀疑我没有在 XAML 中正确声明枚举。我试过了

  CommandParameter = "<fullpath>..MessageTokens.SupplierInvoice>"

但这会在 MVVMLight 中产生 InvalidCastException 。

有没有人有什么建议?

非常感谢杰里米

用解决方案编辑

问题是因为我没有正确引用 CommandParameter 中使用 + 号绑定的内部类。

正确的 XAML 应该是:

<Controls1:RadMenuItem Command="{Binding SalesInvoiceViewModel.OpenInvoiceWindowCommand}" CommandParameter="{x:Static local:DebitInvoiceViewModelBase+MessageTokens.SalesInvoice}" Header="Raise a sales invoice"/>
有帮助吗?

解决方案

您可能想看一下这篇文章,解释如何将枚举值作为命令参数传递: 从 XAML 将枚举值作为命令参数传递.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top