交互触发器不在Keyboard.LostKeyboardFocus
问题描述:
触发我的问题是交互触发器不能在Keyboard.LostKeyboardFocus事件上工作。但是当我从后面的代码中完成工作。 我有一个文本框与下面的代码,交互触发器不在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>
而且在这里的视图模型是代码,
#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
}
问题是,当我提高键盘在代码后面失去焦点。所有其他属性都被正确绑定。但交互触发器不起作用,请帮助。
答
内部的EventTrigger
最终会在它的源代码中调用typeof(TextBox).GetEvent("Keyboard.LostKeyboardFocus")
,并且将返回null
,因为TextBox
没有Keyboard.LostKeyboardFocus
事件。由于Keyboard.LostKeyboardFocus
是附加事件,因此WPF/XAML概念GetEvent
无法将其返回,并且EventTrigger
无法订阅它。
另一方面TextBox
从UIElement
继承LostKeyboardFocus
,所以使用它将工作。
如果在XAML中将'Keyboard.LostKeyboardFocus'更改为'LostKeyboardFocus',它会工作吗? –
@SzabolcsDézsi感谢它的工作方式。 – nikhil