【发布时间】:2016-03-21 14:44:13
【问题描述】:
有谁知道为什么特定于 MVVM Light RelayCommand 泛型类型会导致其 canExecute 始终解析为 false 绑定?为了获得正确的行为,我必须使用一个对象,然后将其转换为所需的类型。
注意:canExecute 被简化为布尔值,用于测试不起作用的块,通常是属性 CanRequestEdit。
不起作用:
public ICommand RequestEditCommand {
get {
return new RelayCommand<bool>(commandParameter => { RaiseEventEditRequested(this, commandParameter); },
commandParameter => { return true; });
}
}
作品:
public ICommand RequestEditCommand {
get {
return new RelayCommand<object>(commandParameter => { RaiseEventEditRequested(this, Convert.ToBoolean(commandParameter)); },
commandParameter => { return CanRequestEdit; });
}
}
XAML:
<MenuItem Header="_Edit..." Command="{Binding RequestEditCommand}" CommandParameter="true"/>
【问题讨论】:
-
我认为 CommandParameter 是一个字符串。
-
你是对的,CommandParameter 是一个字符串。您认为这会对硬编码为返回 true 的 canExecute 产生什么影响?
-
奇怪...你可以尝试输入一个函数吗?类似于: RelayCommand
x = new RelayCommand (req => { string s = "true"; }, req => canExecute());私有 bool canExecute() { return true } -
建议的更改产生了相同的结果。作为旁注,您认为这可能与内联 lambda 有何不同?我知道有时预期的行为会产生不同的结果,所以我只是好奇你的思维过程是什么。
标签: wpf mvvm-light commandbinding commandparameter canexecute