简体   繁体   中英

using ICommand in wpf

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?

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));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM