Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

binding on button c#

<Button Command="{Binding Path=SaveCommand}" />
Comment

binding on button c#

public MyCustomClass
{
    private ICommand _saveCommand;

    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new RelayCommand(
                    param => this.SaveObject(), 
                    param => this.CanSave()
                );
            }
            return _saveCommand;
        }
    }

    private bool CanSave()
    {
        // Verify command can be executed here
    }

    private void SaveObject()
    {
        // Save command execution logic
    }
}
Comment

binding on button c#

/// <summary>
/// A command whose sole purpose is to 
/// relay its functionality to other
/// objects by invoking delegates. The
/// default return value for the CanExecute
/// method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;        

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;           
    }

    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameters)
    {
        return _canExecute == null ? true : _canExecute(parameters);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameters)
    {
        _execute(parameters);
    }

    #endregion // ICommand Members
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity basic public options 
Csharp :: c# program exit 
Csharp :: unity apply bloom of a different color 
Csharp :: blazor clientside cookies 
Csharp :: remove string inside curly braces C# 
Csharp :: create blazor server 
Csharp :: unity datetime to string 
Csharp :: unity deactivate scripts in list 
Csharp :: Advertisement code for unity 
Csharp :: dictionary all key where value c# 
Csharp :: unity create a textbox in inspector 
Csharp :: find gameobject by name in root 
Csharp :: how to access resources in c# 
Csharp :: begininvoke async c# 
Csharp :: check if object has parent unity 
Csharp :: lwjgl fullscreen 
Csharp :: c# file to byte array 
Csharp :: C# random multiple of 5 in range 
Csharp :: method c# 
Csharp :: base c# 
Csharp :: summernote dropdown plugin 
Csharp :: provide inject vue 
Csharp :: c# catch two exceptions in one block 
Csharp :: escape in c# 
Csharp :: Transpose Matrix CSharp 
Csharp :: assert throw 
Csharp :: dateTime first/last 
Csharp :: ioptions mock c# unittest 
Csharp :: wpf binding to static property in code behind 
Csharp :: Go Statement in CSharp 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =