WPF V4 - MVVM Light V4 (BL16 - Mix11) - RelayCommand - CanExecute 不触发
我正在尝试在使用 MVVM-light V3 Sp1 的 WPF v4 应用程序中使用最新预览版的 MVVM-Light (V4 - BL16 - Mix11)。
在我的 ViewModel 中,我定义了以下属性,
private WifiNetwork _selectedNetwork;
public WifiNetwork SelectedNetwork
{
get
{
return _selectedNetwork;
}
set
{
if (_selectedNetwork == value)
return;
_selectedNetwork = value;
RaisePropertyChanged("SelectedNetwork");
}
}
此属性绑定到 View XAML 中的 ListBox 的 SelectedItem。
我在我的 viewModel 中定义了以下 RelayCommand
private RelayCommand _connectCommand;
public RelayCommand ConnectCommand
{
get
{
if (_connectCommand == null)
{
_connectCommand = new RelayCommand(
() => ConnectToSpecifiedNetwork(SelectedNetwork),
() => SelectedNetwork != null && ! SelectedNetwork.IsConnected);
}
return _connectCommand;
}
}
此 RelayCommand 绑定到我的“连接”按钮。
当所选项目尚未连接(IsConnected 为 false)时,应启用连接按钮。
这在 MVVM-Light V3 Sp1 中完美运行。但是当我使用 MIX 11 预览版的 MVVM-Light 时,这似乎不再起作用了。
有什么建议吗?
如果需要,我可以将整个项目上传到某个地方。
I am trying to use the latest Preview version of MVVM-Light (V4 - BL16 - Mix11) in my WPF v4 app that was using MVVM-light V3 Sp1.
In my ViewModel, I have the following property defined
private WifiNetwork _selectedNetwork;
public WifiNetwork SelectedNetwork
{
get
{
return _selectedNetwork;
}
set
{
if (_selectedNetwork == value)
return;
_selectedNetwork = value;
RaisePropertyChanged("SelectedNetwork");
}
}
This property is binded to ListBox's SelectedItem in the View XAML.
I have the following RelayCommand defined in my viewModel
private RelayCommand _connectCommand;
public RelayCommand ConnectCommand
{
get
{
if (_connectCommand == null)
{
_connectCommand = new RelayCommand(
() => ConnectToSpecifiedNetwork(SelectedNetwork),
() => SelectedNetwork != null && ! SelectedNetwork.IsConnected);
}
return _connectCommand;
}
}
This RelayCommand is binded to my "Connect" button.
When the selected item is not already connected (IsConnected is false), then the connect button should enable.
This worked perfect in MVVM-Light V3 Sp1. But when I use the MIX 11 preview version of MVVM-Light, this doesn't seem to work anymore.
Any suggestions?
I can upload my entire project somewhere if needed..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是删除了对 CommandManager 的调用。如果您将其添加回来,则无需手动引发它。但这在 silverlight 中不起作用。以下是似乎已删除的 V3 代码:
链接到 V3 RelayCommand
链接到 V4 RelayCommand
The reason is that the call to the CommandManager was removed. If you add this back, you won't have to manually raise it. This won't work in silverlight though. Here is the code from V3 that appears to be removed:
Link to V3 RelayCommand
Link to V4 RelayCommand
为了解决这个问题,我必须在 INPC 属性上调用“RaiseCanExecuteChanged()”方法,如下所示,但
在 MVVM-Light V3 Sp1 中我不必执行此操作(在命令上手动引发 RaiseCanExecuteChanged)。
In order to fix this issue, I had to call "RaiseCanExecuteChanged()" method on my INPC property as shown below
I didn't have to do this (manually raise the RaiseCanExecuteChanged on a command) in MVVM-Light V3 Sp1.