将枚举绑定到MVVM Light RelayCommand <t> CommandParameter </t>

时间:2011-02-12 18:06:36

标签: wpf mvvm-light commandparameter

我正在尝试将XAML中的命令参数绑定到RelayCommand <MessageTokens&gt;如下所示,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)

如果我将参数更改为RelayCommand <string&gt;而不是RelayCommand <MessageTokens&gt;代码运行正常。

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

我怀疑我没有在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"/>

1 个答案:

答案 0 :(得分:2)

您可能需要查看此帖子,了解如何将枚举值作为命令参数传递:Passing an enum value as command parameter from XAML