The code below is MVVM class for my WPF application. In my MainWindow.xaml.cs file, MainWindow() constructor, I have done like this.
oneWayAuth = new OneWayAuthentication(); DataContext = oneWayAuth My Mainwindow contains multiple buttons and I need to assign events by binding like this using ICommand,
MainWindow.xaml
<Grid> <Button> Command="{Binding ClickCommand}" Content="Button" Grid.Row="1" Name="button1" /> </Grid> Inside event for button, I should be able to access oneWayAuth.RandomNumber property, so that I can change it.
I tried to use the method below. But I could not pass Action delegate with return type.
OneWayAuthentication.cs
public class OneWayAuthentication : INotifyPropertyChanged { private string certData; private string isVerifiedCert; private string randomNumber; private string response; private string isVerifiedRes; private string resultAuth; public string RandomNumber { get { return randomNumber; } set { randomNumber = value; NotifyPropertyChanged("RandomNumber"); } } public ICommand ClickCommand { get { ICommand intfaceCmnd = new CommandHandler(() => Execute(), () => Switch()); return intfaceCmnd; } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion #region Private Helpers private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion public bool Switch() { return true; } public void Execute() { this.RandomNumber = "this is random number"; } } CommandHandler.cs
public delegate bool delCanExecute(object parameter); public class CommandHandler:ICommand { private Action _action; private Action _canExecute; public CommandHandler(Action action1, Action action2) { _action = action1; _canExecute = action2; } public bool CanExecute(object parameter) { bool res = _canExecute(); return res; } public void Execute(object parameter) { _action(); } public event EventHandler CanExecuteChanged; }
bool = _canExecute();Cannot implicitly conver type 'void' to 'bool', in CommandHandler.cs