-1

I want to to replace an event with a an ICommand:

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    textBox2.Text = textBox1.Text;
}

Is that possible to replace this event with a command, and how can I do so?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Essam Ali
  • 25
  • 7
  • 3
    Possible duplicate of [WPF ICommand MVVM implementation](https://stackoverflow.com/questions/1468791/wpf-icommand-mvvm-implementation) – Or Yaacov Feb 08 '19 at 14:30
  • 4
    Why Command ? In your XAML for textbox2.Text you can write Text={Binding ElementName=textBox1, Path=Text, Mode=OneWay} – Nawed Nabi Zada Feb 08 '19 at 14:30
  • `TextChanged` in many cases can be replaced with code in setter of property bound to `Text` (with `UpdateSourceTrigger="PropertyChange"`). In your case you don't even need that. You can either bind one textbox to another or bind them both to same property (don't forgot about `INotifyPropertyChanged`). – Sinatr Feb 08 '19 at 15:37

1 Answers1

0

ButtonBase types have the ICommand Command and object CommandParameter dependency properties built in. You can absolutely make your own in several simple ways but here's my 2 top suggestions.

You can make you're own CommandableTextBox control, that basically adds 2 dependency properties to the control: ICommand Command and object CommandParameter.

Or you can make an attachable property (which is what I highly suggest) that allows you to add a command and command parameter to particular types. This is more work upfront but much cleaner and easier to use and you could add it to existing TextBox controls.

I've written the attachable TextChangedCommand for you. Here's what it looks like in XAML. In my solution I've added the namespace to the XAML as such but you'll need to make sure you're path is correct for yours.

xmlns:attachable="clr-namespace:Question_Answer_WPF_App.Attachable"

<TextBox attachable:Commands.TextChangedCommand="{Binding MyCommand}"
         attachable:Commands.TextChangedCommandParameter="{Binding MyCommandParameter}"/>

And here's the Attachable Properties for you:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Question_Answer_WPF_App.Attachable
{
    public class Commands
    {
        public static ICommand GetTextChangedCommand(TextBox textBox) 
            => (ICommand)textBox.GetValue(TextChangedCommandProperty);
        public static void SetTextChangedCommand(TextBox textBox, ICommand command) 
            => textBox.SetValue(TextChangedCommandProperty, command);
        public static readonly DependencyProperty TextChangedCommandProperty =
            DependencyProperty.RegisterAttached(
                "TextChangedCommand", 
                typeof(ICommand), 
                typeof(Commands),
                new PropertyMetadata(null, new PropertyChangedCallback((s, e) =>
                {
                    if (s is TextBox textBox && e.NewValue is ICommand command)
                    {
                        textBox.TextChanged -= textBoxTextChanged;
                        textBox.TextChanged += textBoxTextChanged;    
                        void textBoxTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
                        {
                            var commandParameter = GetTextChangedCommandParameter(textBox);
                            if (command.CanExecute(commandParameter))
                                command.Execute(commandParameter);
                        }
                    }
                })));

        public static object GetTextChangedCommandParameter(TextBox textBox) 
            => textBox.GetValue(TextChangedCommandParameterProperty);
        public static void SetTextChangedCommandParameter(TextBox textBox, object commandParameter) 
            => textBox.SetValue(TextChangedCommandParameterProperty, commandParameter);
        public static readonly DependencyProperty TextChangedCommandParameterProperty =
            DependencyProperty.RegisterAttached("TextChangedCommandParameter", typeof(object), typeof(Commands), new PropertyMetadata(null));
    }
}
Michael Puckett II
  • 6,586
  • 5
  • 26
  • 46