我在ViewModels中有一堆ICommand实现。我想制作一个可以从ViewModel中获取所有“ ICommand”属性的方法,然后调用“ CanExecute”方法来更新所有命令按钮的状态。
private ICommand _commandInsert;
public ICommand CommandInsert { get { if (_commandInsert == null) { _commandInsert = new RelayCommand<object>(async (parameter) => await InsertCallBack(parameter), c => true); } return _commandInsert; } }
然后在不同的事件(例如重新加载我的收藏集或更改我调用的连接状态)上
//TODO: get this stupid thing working!
public override void UpdateCanExecute()
{
//the foreach and property query works fine, i can see all the ICommand properties but can never get the value
foreach (PropertyInfo prop in this.GetType().GetProperties().Where(p => typeof(ICommand).IsAssignableFrom(p.PropertyType) && p.PropertyType.Name == "ICommand"))
{
try
{
//tried a every different variation to get the value from the ICommand property and get so many random errors
//I have all the ICommand properties and I need to cast them to my RelayCommand and then call the RaiseCanExecute method
//I hate my life and myself because I can't get this to work
//!!!this is what i'm trying to accomplish!!!
ICommand cmd = (ICommand)prop.GetValue(prop, null);
RelayCommand<object> rly = (RelayCommand<object>)cmd;
rly?.RaiseCanExecuteChanged();
}
catch (Exception e)
{
}
}
}