I have defined a button in my View (AXML) with the following code:
<Button Content="Fetch Data" Command="{Binding readInventoryFilesCommand}" CommandParameter="{Binding Path=Text, ElementName=browseFolderTextBox}" Name="button1" />
The button works as expected but I would like it to be enabled/disabled if a ListView has been populated with elements.
- If no elements in the listView --> Button
IsEnabledset tofalse - If there are elements in the listView --> Button
IsEnabledset totrue
I tried to play around with IsEnabled="{Binding ... but I am not getting code completion in Visual Studio C# Express and it is hard to guess the alternatives.
The command looks like this:
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
}
EDIT This is the current code after Bobs advice:
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);
}
}
It generates the following errors:
An explicit interface implementation of an event must use event accessor syntax