在Keyboard.LostKeyboardFocus上未触发交互触发器

时间:2017-10-12 22:17:04

标签: wpf xaml mvvm

我的问题是交互触发器无法处理Keyboard.LostKeyboardFocus事件。但是,当我从代码背后做它时,它起作用。 我有一个包含以下代码的文本框,

                      <TextBox
                        Text="{Binding PostRunPlateNotes, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                        Height="113"
                        Width="Auto"
                        ScrollViewer.HorizontalScrollBarVisibility="Auto"
                        ScrollViewer.VerticalScrollBarVisibility="Auto">
                        <i:Interaction.Triggers>
                            <i:EventTrigger
                                EventName="Keyboard.LostKeyboardFocus">
                                <i:InvokeCommandAction
                                    Command="{Binding SavePlateNotesCommand}"></i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TextBox>

在ViewModel中这里是代码

#region CommandData
        private ICommand _savePlateNotesCommand;
        public ICommand SavePlateNotesCommand
        {
            get
            {
                return _savePlateNotesCommand ?? (_savePlateNotesCommand = new RelayCommand(OnSaveClick));
            }
            set
            {
                _savePlateNotesCommand = value;
                OnPropertyChanged("SavePlateNotesCommand");
            }
        }
        #endregion

在后面的代码中,

 public partial class RunInformationUserControl : UserControl, INotifyPropertyChanged
    {
        #region Accessors
        public RunInformationParameters RunInfoParams { get; set; }
        public RunInformationViewModel RunViewModel { get; set; }
        #endregion

        #region Constructor
        public RunInformationUserControl()
        {
            InitializeComponent();
            RunViewModel = null;
        }
        #endregion

        #region Methods
        public void SetParameters(RunInformationParameters parameters)
        {
            this.RunInfoParams = parameters;
            RunViewModel = new RunInformationViewModel(RunInfoParams);
            this.DataContext = RunViewModel;
        }

        #endregion

        #region EventHandlers
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (RunViewModel != null)
            {
                OnPropertyChanged("runInfoParams");
            }
        }

        #endregion

        #region INotifyPropertyChangedEvent
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }

问题是当我从键盘后面提起键盘失去焦点时,它起作用。所有其他属性都正确绑定。但是交互触发器无效,请帮忙。

1 个答案:

答案 0 :(得分:0)

内部EventTrigger最终会在其源代码中调用typeof(TextBox).GetEvent("Keyboard.LostKeyboardFocus"),并且会返回null,因为TextBox没有Keyboard.LostKeyboardFocus个事件。由于Keyboard.LostKeyboardFocus是附加事件,因此是WPF / XAML概念,GetEvent无法返回它,EventTrigger无法订阅它。

另一方面,TextBox继承了LostKeyboardFocus的{​​{1}},因此使用它会有效。