![](/img/trans.png)
[英]How to call a Button press Event even with the press of the Enter key on keyboard
[英]How to prevent multiple consecutive 'Enter' key press on a button
我有一個按鈕,當我通過鍵盤按Enter鍵時,將執行button命令。 當我連續按下“ Enter”鍵時,該命令還會執行多次,這是我不希望的。
我想將行為限制為僅執行一次命令,即使在多次“ Enter”鍵按下期間也是如此。 有人可以幫忙嗎?
View.xaml
<Button x:Name="btnSearch" IsDefault="True" Content="Search" Command="{Binding SearchButtonCommand}">
<Button.InputBindings>
<KeyBinding Command="{Binding Path=SearchButtonCommand}" Key="Return" />
</Button.InputBindings>
</Button>
ViewModel.cs
public ICommand SearchButtonCommand
{
get { return new DelegateCommand(SearchButtonExecutionLogic); }
}
達到目標的最簡單方法是在ViewModel的命令實現中引入並檢查一些標志isSearchRunning
:
private bool isSearchRunning = false;
private void SearchButtonCommandImpl()
{
if (isSearchRunning)
{
return;
}
try
{
isSearchRunning = true;
//Do stuff
}
finally
{
isSearchRunning = false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.