我正在使用 MVVM 模式。我是我的观点,我有个人详细信息的文本框,其中之一是 idBox。此外,该视图由几个按钮组成,其中之一是 editModeBtn。
我希望只有在 idBox 中有一个有效的 int 时才启用 editModeBtn。
我的 editBtn 的 Xaml(在视图内)如下所示:
<Button x:Name="editModeBtn" Content="Edit" Command="{Binding ChangeToEditScreenCommand}" CommandParameter="{Binding ElementName=idBox, Path=Text}"></Button>
在相应的viewModel中,我有以下代码:
private RelayCommand<string> _changeToEditScreenCommand;
public RelayCommand<string> ChangeToEditScreenCommand
{
get
{
if (_changeToEditScreenCommand == null)
{
_changeToEditScreenCommand = new RelayCommand<string>((param) => ChangeToEditScreen(), (param) => CanEdit(param));
}
return _changeToEditScreenCommand;
}
}
此外,在 CanExecute 方法(在我的情况下为 CanEdit)中,我想检查参数(id)是否设置为有效的 int,然后返回 true。假的,否则。
private bool CanEdit(string currentInsertedId)
{
int idValue;
bool result = Int32.TryParse(currentInsertedId, out idValue);
if (result)
{
if (idValue > 0) { return true; };
return false;
}
return false;
}
基本上,我希望在每次从 idBox 写入或删除某些内容时调用该命令的 canExecute 方法。我应该把命令的 RaiseCanExecuteChanged() 放在哪里?如果我没有使用过 MVVM,我可以将它放在 textBox textChanged 事件中,但这里不是这样。从未使用过 RaiseCanExecuteChanged,所以只想确保。谢谢!