用于在MVVM中打开第二个窗口的命令 [英] Command to open a second window in MVVM
问题描述
我一直在撞墙撞墙。
到现在为止我已经使用了背后的代码按钮逻辑(我知道很糟糕。)所以我决定为我的按钮创建一个命令。我的命令代码:
< Button Name =AddUserCommand ={Binding Path = AddUserCommand}CommandParameter =AddUserContent =New UserMargin =20/>
和我的命令代码:
public ICommand AddUserCommand {get;组; }
public bool CanExecute(对象参数)
{
if (参数!= null)
{
返回true;
}
其他
{
返回false;
}
}
公共活动EventHandler CanExecuteChanged
{
add {CommandManager.RequerySuggested + = value; }
remove {CommandManager.RequerySuggested - = value; }
}
public void执行(字符串参数)
{
if(parameter ==AddUser)
{
var window = new AddUser();
window.Show();
}
}
我设置了断点,我的命令代码永远不会被触及。
我看不到我的问题所在。
提前致谢!
Hi,
I have been banging my head against the wall on this one.
Until now I have used the code behind for button logic (I know bad.). so I decided to creat a command for my buttons. My command code:
<Button Name="AddUser" Command="{Binding Path=AddUserCommand}" CommandParameter="AddUser" Content="New User" Margin="20" />
And My command code:
public ICommand AddUserCommand { get; set; }
public bool CanExecute(object parameter)
{
if (parameter != null)
{
return true;
}
else
{
return false;
}
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(string parameter)
{
if (parameter == "AddUser")
{
var window = new AddUser();
window.Show();
}
}
I have set break points and my command code never gets touched.
I don't see where My problem is.
Thanks in advance!
推荐答案
在您的ctor中执行以下操作(您可能必须实现DelegateCommand - 它存在于Prism中):
In your ctor do the following (you might have to implement the DelegateCommand - it's present in Prism):
AddUserCommand = new DelegateCommand(Execute, CanExecute);
同时更改以下内容:
*在Execute to object中更改参数类型并将其强制转换功能内
*摆脱CanExecuteChanged
*考虑不使用标准杆执行中的Ameter和XAML,因为你不需要它
Also change the following:
* Change parameter type in Execute to object and cast it within the function
* Get rid of CanExecuteChanged
* Consider not using the parameter at all in Execute and XAML, because you don't need it
这篇关于用于在MVVM中打开第二个窗口的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!