簡體   English   中英

在 wpf 中使用 ICommand

[英]using ICommand in wpf

我想用ICommand替換事件:

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

是否可以用命令替換此事件,我該怎么做?

ButtonBase類型具有內置的ICommand Commandobject CommandParameter依賴屬性。您絕對可以通過幾種簡單的方式創建自己的類型,但這里是我的 2 個最重要的建議。

您可以創建自己的CommandableTextBox控件,這基本上向控件添加了 2 個依賴屬性: ICommand Commandobject CommandParameter

或者您可以創建一個可附加的屬性(這是我強烈建議的),允許您向特定類型添加命令和命令參數。 這是更多的前期工作,但更清晰、更易於使用,您可以將其添加到現有的TextBox控件中。

我已經為您編寫了可附加的 TextChangedCommand。 這是它在 XAML 中的樣子。 在我的解決方案中,我已將命名空間添加到 XAML,但您需要確保您的路徑是正確的。

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

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

這是您的可附加屬性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM