有谁知道为什么使用MVVM Light RelayCommand通用类型会导致其canExecute始终解析为绑定错误?为了获得正确的行为,我必须使用一个对象,然后将其转换为所需的类型。MVVM Light canExecute始终为false,并且RelayCommand <bool> not RelayCommand <object>
注意: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是作为一个字符串。 – sexta13
你是正确的,CommandParameter是作为一个字符串。你如何认为这会对canExecute产生影响,而硬编码会返回true? – Rock
奇怪......你可以尝试放入一个函数吗?例如: RelayCommand x = new RelayCommand (req => {string s =“true”;},req => canExecute()); private bool canExecute() { return true } –
sexta13