MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.32k stars 2.45k forks source link

TextBoxHelper.ButtonCommand not firing #3363

Closed CarterAAelect closed 5 years ago

CarterAAelect commented 5 years ago

Hi All, I'm having a problem getting the Textbox Helper button command to work.

XAML

<TextBox Style="{DynamicResource MetroButtonTextBox}"
                             Controls:TextBoxHelper.ButtonCommand="{Binding TextBoxButtonCmd, Mode=OneWay}"
                             Controls:TextBoxHelper.ButtonContent="s"
                             Controls:TextBoxHelper.Watermark="Custom icon style" />

Class

    public class SimpleCommand : ICommand
    {
        public Predicate<object> CanExecuteDelegate { get; set; }
        public Action<object> ExecuteDelegate { get; set; }

        public bool CanExecute(object parameter)
        {
            if(CanExecuteDelegate != null)
                return CanExecuteDelegate(parameter);
            return true; // if there is no can execute default to true
        }

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

        public void Execute(object parameter)
        {
            if(ExecuteDelegate != null)
                ExecuteDelegate(parameter);
        }
    }

ICommand

        private ICommand textBoxButtonCmd;

        public ICommand TextBoxButtonCmd
        {
            get
            {
                return this.textBoxButtonCmd ?? (this.textBoxButtonCmd = new SimpleCommand
                {
                    CanExecuteDelegate = x => true,
                    ExecuteDelegate = async x =>
                    {
                        if(x is string)
                        {
                            await ((MetroWindow)Application.Current.MainWindow).ShowMessageAsync("Wow, you typed Return and got", (string)x);
                        }
                        else if(x is TextBox)
                        {
                            await ((MetroWindow)Application.Current.MainWindow).ShowMessageAsync("TextBox Button was clicked!", string.Format("Text: {0}", ((TextBox)x).Text));
                        }
                        else if(x is PasswordBox)
                        {
                            await ((MetroWindow)Application.Current.MainWindow).ShowMessageAsync("PasswordBox Button was clicked!", string.Format("Password: {0}", ((PasswordBox)x).Password));
                        }
                    }
                });
            }
        }

using the above, when the textbox button is press, nothing executes. I'm sure I'm missing something stupid, but I'm just not seeing it. Thanks

punker76 commented 5 years ago

@CarterAAelect Did you set the DataContext of the view to the class which has the TextBoxButtonCmd?

CarterAAelect commented 5 years ago

I Don't believe so, could you elaborate on how to do that? thanks for the help

punker76 commented 5 years ago

@CarterAAelect For example:

https://github.com/MahApps/MahApps.Metro/blob/c3ff665c873cec5ffadbf03a73a58bf5bcfbe39b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindow.xaml.cs#L20-L21

CarterAAelect commented 5 years ago

AH! that makes sense... thank you for the help