2
我試圖在XAML命令參數綁定爲一個RelayCommand <MessageTokens
>如下,其中MessageTokens是一個枚舉綁定枚舉到MVVM光RelayCommand <T> CommandParameter
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
>而不是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"/>
你可以顯示OpenInvoiceWindow和CanExecuteOpenInvoiceWindowCommand的代碼嗎? – Robaticus 2011-02-12 19:19:54
你提供的堆棧跟蹤有什麼異常? – 2011-02-13 05:20:05