我在我的View
(AXML)中定义了一个按钮,代码如下:
<Button Content="Fetch Data" Command="{Binding readInventoryFilesCommand}" CommandParameter="{Binding Path=Text, ElementName=browseFolderTextBox}" Name="button1" />
ListView
该按钮按预期工作,但如果 a已填充元素,我希望启用/禁用它。
- 如果 listView 中没有元素 --> Button
IsEnabled
设置为false
- 如果 listView 中有元素 --> Button
IsEnabled
设置为true
我试图玩弄,IsEnabled="{Binding ...
但我没有进入code completion
,Visual Studio C# Express
很难猜出替代方案。
该命令如下所示:
internal class ReadInventoryFilesCommand : ICommand
{
public ReadInventoryFilesCommand(ResourceViewModel viewModel)
{
_viewModel = viewModel;
}
private ResourceViewModel _viewModel;
#region ICommand Members
event EventHandler ICommand.CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
bool ICommand.CanExecute(object parameter)
{
return _viewModel.CanUpdate;
}
void ICommand.Execute(object parameter)
{
_viewModel.ReadInventroyFiles(parameter.ToString());
}
#endregion
}
编辑 这是 Bobs 建议后的当前代码:
internal class ReadInventoryFilesCommand : ICommand
{
public ReadInventoryFilesCommand(ResourceViewModel viewModel)
{
_viewModel = viewModel;
_viewModel.PropertyChanged+= (s,e) => {
if (e.PropertyName == "CanUpdate") RaiseCanExecuteChanged();
};
}
private ResourceViewModel _viewModel;
event EventHandler ICommand.CanExecuteChanged;
bool ICommand.CanExecute(object parameter)
{
return _viewModel.CanUpdate;
}
void ICommand.Execute(object parameter)
{
_viewModel.ReadInventroyFiles(parameter.ToString());
}
void RaiseCanExecuteChanged() {
var handler = this.CanExecuteChanged;
if (handler != null) handler(this,EventArgs.Empty);
}
}
它会产生以下错误:
An explicit interface implementation of an event must use event accessor syntax