Dan*_*nte 13 c# wpf prism button icommand
我正在使用MVVM在WPF中开发应用程序,但我坚持使用ICommand对象.
我有一个包含一些按钮的窗口,因此,我将它们绑定到XAML中各自的ICommand,如下所示:
<Button Command="{Binding DoSomethingCommand}" Content="Do Something" />
Run Code Online (Sandbox Code Playgroud)
然后,在我的视图模型类中,我写了以下内容:
public class MyViewModel : ObservableObject
{
private bool isDoSomethingButtonEnabled = false;
....
public ICommand DoSomethingCommand
{
get;
private set;
}
....
....
public MyViewModel()
{
DoSomethingCommand = new DelegateCommand<String>(this.OnDoSomething, this.CanDoSomething);
}
private void OnDoSomething(String arg)
{
}
private bool CanDoSomething(String arg)
{
return isDoSomethingButtonEnabled;
}
....
}
Run Code Online (Sandbox Code Playgroud)
所以,因为我需要在第一次打开窗口时没有启用我的按钮,所以我将变量设置isDoSomethingButtonEnabled为false.和它的作品,该按钮在开始时禁用的,但我的问题是,当我改变变量isDoSomethingButtonEnabled来true在运行时我的按钮仍然被禁用.
我甚至做了一些测试,改变变量后isDoSomethingButtonEnabled到true,印刷的结果DoSomethingCommand.CanExecute(),它显示了"真"!
所以,我该怎么办才能启用我的按钮?
先感谢您
nem*_*esv 14
接口 上有一个名为CanExecuteChanged的事件ICommand:
发生影响命令是否应执行的更改时发生.
使用Prism DelegateCommand,您可以使用以下RaiseCanExecuteChanged方法引发此事件:
public void SomeMethod()
{
//do some work
isDoSomethingButtonEnabled = true;
DoSomethingCommand.RaiseCanExecuteChanged();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13571 次 |
| 最近记录: |