【问题标题】:Bind ICommand to enter pressed绑定 ICommand 进入按下
【发布时间】:2012-07-24 07:47:33
【问题描述】:

所以我有一个带有基本视图的 Windows 8 应用程序: 示例:

 <TextBox Text="{Binding UserName}"/>
 <PasswordBox Text="{Binding Password}"/>
 <Button Content="Sign In" Command="{Binding Login}"/>

还有一个实现了 ICommand 的视图模型。


public class LoginViewModel : ViewModelBase
{
    public ICommand Login { get; set; }
    //Other properties

    public LoginViewModel()
    {
       Login = ExecuteLogin;
    }

    public void ExecuteLogin()
    {
         //Logic
    }
}

一切正常。但是现在我想在用户在密码框中按 enter 时触发 ICommand,只是为了更好的用户体验。有人知道怎么做吗?

【问题讨论】:

    标签: mvvm-light windows-runtime icommand


    【解决方案1】:

    是的,你需要的是这个类:

    namespace SL
    {
        public sealed class EnterKeyDown
        {
            #region Properties
    
            #region Command
    
            /// <summary>
            /// GetCommand
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static ICommand GetCommand(DependencyObject obj)
            {
                return (ICommand)obj.GetValue(CommandProperty);
            }
    
            /// <summary>
            /// SetCommand
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="value"></param>
            public static void SetCommand(DependencyObject obj, ICommand value)
            {
                obj.SetValue(CommandProperty, value);
            }
    
            /// <summary>
            /// DependencyProperty CommandProperty
            /// </summary>
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(EnterKeyDown), new PropertyMetadata(null, OnCommandChanged));
    
            #endregion Command
    
            #region CommandParameter
    
            /// <summary>
            /// GetCommandParameter
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static object GetCommandParameter(DependencyObject obj)
            {
                return (object)obj.GetValue(CommandParameterProperty);
            }
    
            /// <summary>
            /// SetCommandParameter
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="value"></param>
            public static void SetCommandParameter(DependencyObject obj, object value)
            {
                obj.SetValue(CommandParameterProperty, value);
            }
    
            /// <summary>
            /// DependencyProperty CommandParameterProperty
            /// </summary>
            public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(EnterKeyDown), new PropertyMetadata(null, OnCommandParameterChanged));
    
            #endregion CommandParameter
    
            #region HasCommandParameter
    
            private static bool GetHasCommandParameter(DependencyObject obj)
            {
                return (bool)obj.GetValue(HasCommandParameterProperty);
            }
    
            private static void SetHasCommandParameter(DependencyObject obj, bool value)
            {
                obj.SetValue(HasCommandParameterProperty, value);
            }
    
            private static readonly DependencyProperty HasCommandParameterProperty =
                DependencyProperty.RegisterAttached("HasCommandParameter", typeof(bool), typeof(EnterKeyDown), new PropertyMetadata(false));
    
            #endregion HasCommandParameter
    
            #endregion Propreties
    
            #region Event Handling
    
            private static void OnCommandParameterChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                SetHasCommandParameter(o, true);
            }
    
            private static void OnCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                FrameworkElement element = o as FrameworkElement;
                if (element != null)
                {
                    if (e.NewValue == null)
                    {
                        element.KeyUp -= FrameworkElementKeyUp;
                    }
                    else if (e.OldValue == null)
                    {
                        element.KeyUp += FrameworkElementKeyUp;
                    }
                }
            }
    
            private static void FrameworkElementKeyUp(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == VirtualKey.Enter)
                {
                    var o = sender as DependencyObject;
                    var command = GetCommand(sender as DependencyObject);
    
                    var element = e.OriginalSource as FrameworkElement;
                    if (element != null)
                    {
                        // If the command argument has been explicitly set (even to NULL) 
                        if (GetHasCommandParameter(o))
                        {
                            var commandParameter = GetCommandParameter(o);
    
                            // Execute the command 
                            if (command.CanExecute(commandParameter))
                            {
                                command.Execute(commandParameter);
                            }
                        }
                        else if (command.CanExecute(element.DataContext))
                        {
                            command.Execute(element.DataContext);
                        }
                    }
                }
            }
    
            #endregion
        }
    }
    

    XAML 命名空间:

    xmlns:sl="clr-namespace:SL;"
    

    XAML 代码:

    <TextBox sl:EnterKeyDown.Command="{Binding SearchClickCommand}"                              
              Text="{Binding Input, Mode=TwoWay, 
                 NotifyOnValidationError=True, ValidatesOnDataErrors=True, 
                 ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}" />
    
    <Button Content="Search"
            Command="{Binding SearchClickCommand}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2021-04-02
      • 1970-01-01
      • 2015-04-02
      • 2016-04-23
      • 2015-11-13
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多