【发布时间】:2014-09-27 09:19:38
【问题描述】:
我有一个文本框,我为该文本框附加了一个 keydown 事件。一切正常,但我只是注意到当我按下“Backspace”和“Delete”键时,没有调用绑定命令。
我的视图 xaml 文件:-
<TextBox x:Name="textBox" Width="500" Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{BindingPath=TextBoxKeyDownEvent}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
我的 ViewModel cs 文件:-
//TextBox Key Down Event Handler
private DelegateCommand _textBoxKeyDownEvent;
public ICommand TextBoxKeyDownEvent
{
get
{
if (_textBoxKeyDownEvent == null)
{
_textBoxKeyDownEvent = new DelegateCommand(TextBoxKeyDownEventHandler);
}
return _textBoxKeyDownEvent;
}
set { }
}
谁能给我一些建议
【问题讨论】:
-
EventToCommand是您需要深入研究的黑匣子。 -
您可以使用 KeyBinding - 但我猜您的 TextBoxText 属性已更新。为什么在文本更改时收到通知时需要捕获 Keys?
-
@kallocaion:感谢您的回答。 Bur 对于我的情况 Binding textProperty 将不起作用。因为程序会动态更改文本框的内容,然后我不想调用我的 eventHandler。我有其他事件与同一组件相关联,我无法切换到其他事件,例如 keyup。