System.Windows.Data.DataCommand.GetDataItemCommand C# (CSharp) Method

GetDataItemCommand() public static method

public static GetDataItemCommand ( object dataItem, string commandName ) : ICommand
dataItem object
commandName string
return ICommand
        public static ICommand GetDataItemCommand(object dataItem, string commandName)
        {
            if (String.IsNullOrEmpty(commandName)) {
                return null;
            }

            BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
            MethodInfo method = dataItem.GetType().GetMethod(commandName, flags);

            if (method == null) {
                return null;
            }

            return new DataCommand(dataItem, method);
        }

Usage Example

        /// <summary>
        /// Gets a command by name.
        /// </summary>
        /// <param name="commandName">The name of the command to lookup.</param>
        /// <returns>The command if its supported; null otherwise.</returns>
        protected virtual ICommand GetCommand(string commandName)
        {
            object dataItem = DataContext;

            if (dataItem == null)
            {
                return(null);
            }

            return(DataCommand.GetDataItemCommand(dataItem, commandName));
        }