【发布时间】:2010-12-07 22:30:25
【问题描述】:
我的 WPF 应用程序中有一个搜索字段,其中包含一个包含命令绑定的搜索按钮。这很好用,但是当在键盘上按下回车键时,如何对文本字段使用相同的命令绑定?我所看到的示例都是使用带有 KeyDown 事件处理程序的 Code behind。是否有一种聪明的方法可以使这项工作仅适用于 xaml 和命令绑定?
【问题讨论】:
标签: c# wpf xaml commandbinding
我的 WPF 应用程序中有一个搜索字段,其中包含一个包含命令绑定的搜索按钮。这很好用,但是当在键盘上按下回车键时,如何对文本字段使用相同的命令绑定?我所看到的示例都是使用带有 KeyDown 事件处理程序的 Code behind。是否有一种聪明的方法可以使这项工作仅适用于 xaml 和命令绑定?
【问题讨论】:
标签: c# wpf xaml commandbinding
Prism 参考实现包含您所追求的实现。
基本步骤是:
这使您可以像这样使用行为:
<TextBox prefix:EnterKey.Command="{Binding Path=SearchCommand}" />
【讨论】:
您可以使用按钮的 IsDefault 属性:
<Button Command="SearchCommand" IsDefault="{Binding ElementName=SearchTextBox,
Path=IsKeyboardFocused}">
Search!
</Button>
【讨论】:
仅当您已经将按钮绑定到命令时,接受的答案才有效。
为避免此限制,请使用 TextBox.InputBindings:
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding Path=MyCommand}"></KeyBinding>
</TextBox.InputBindings>
【讨论】:
我尝试了 Greg Samson 的 TextBox.Inputs 解决方案,但收到一个错误消息,提示我只能通过依赖属性绑定到 textinputs。 最后我找到了下一个解决方案。
创建一个名为 CommandReference 的类,如下所示:
public class CommandReference : Freezable, ICommand
{
public CommandReference()
{
//
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference), new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#region ICommand Members
public bool CanExecute(object parameter)
{
if (Command != null)
return Command.CanExecute(parameter);
return false;
}
public void Execute(object parameter)
{
Command.Execute(parameter);
}
public event EventHandler CanExecuteChanged;
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CommandReference commandReference = d as CommandReference;
ICommand oldCommand = e.OldValue as ICommand;
ICommand newCommand = e.NewValue as ICommand;
if (oldCommand != null)
{
oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
}
if (newCommand != null)
{
newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
}
}
#endregion
#region Freezable
protected override Freezable CreateInstanceCore()
{
throw new NotImplementedException();
}
#endregion
}
在 Xaml 中将此添加到 UserControl 资源中:
<UserControl.Resources>
<Base:CommandReference x:Key="SearchCommandRef" Command="{Binding Path = SomeCommand}"/>
实际的 TextBox 如下所示:
<TextBox Text="{Binding Path=SomeText}">
<TextBox.InputBindings>
<KeyBinding Command="{StaticResource SearchCommandRef}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
我不记得我从哪里得到这个代码,但这个网站也解释了它;
【讨论】:
<TextBox Text="{Binding SerachString, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding SearchCommand}" Key="Enter" />
</TextBox.InputBindings>
</TextBox>
这应该可以正常工作。100%
【讨论】: