<TextBox Width="120" Margin="20,0" HorizontalAlignment="Left"
KeyDown="{x:Bind ViewModel.AddNewTag}"
PlaceholderText="Add New Tag"
Text="{x:Bind ViewModel.NewTagToAdd, Mode=TwoWay}" />
public void AddNewTag(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
var newTag = ((TextBox)sender).Text;
if (NewTagDoesNotExistInCurrentLists(newTag))
{
var tagDTO = new TagDTO { UserName = UserName, TagName = newTag };
NewTagToAdd = null;
TagsAvailableForSelecting.Add(tagDTO);
TagsAvailableForSelecting.Sort(x => x.TagName, true);
}
}
}
当我将一个单词键入文本框并按回车键时,该方法被调用两次。使用模板10。
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0",
"Microsoft.Xaml.Behaviors.Uwp.Managed": "1.1.0",
"Newtonsoft.Json": "8.0.3",
"Template10": "1.1.*",
"WindowsAzure.Storage": "7.1.2"
},
"frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
答案 0 :(得分:2)
正如伊戈尔所说,使用e.Handled = true
告诉系统你已经完成了所需的一切。
public void AddNewTag(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
var newTag = ((TextBox)sender).Text;
if (NewTagDoesNotExistInCurrentLists(newTag))
{
var tagDTO = new TagDTO { UserName = UserName, TagName = newTag };
NewTagToAdd = null;
TagsAvailableForSelecting.Add(tagDTO);
TagsAvailableForSelecting.Sort(x => x.TagName, true);
}
e.Handled = true;
}
}
发布为社区维基。