[英]Preventing WPF form submit on pressing Enter key
我有一个具有多个控件(文本框,组合框,按钮等)的WPF XAML屏幕。 后面的代码在C#中。 我为其中一个按钮保留了IsDefault="True"
。 因此,如果用户在任何文本框中按Enter键,则将提交表单。
我只需要为一个特定的文本框使用Enter键提交表单。 如果用户在其他任何文本框中按Enter键,则我不希望提交表单。
我知道我可以使用后面的代码(即*.xaml.cs
)来实现此*.xaml.cs
。 但是,如何使用MVVM设计模式实现这一目标?
您可能不在乎用户按下的是哪个文本框,而只是希望填写完整的表格。 在这种情况下,请执行验证并使用绑定禁用按钮。
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
_saveCommand = new RelayCommand(x => Save(), CanSave);
return _saveCommand;
}
}
private void CanSave(object sender)
{
// Validate properties, ensure viewmodel is in savable state.
// Maybe you implemented IDataErrorInfo?
if (Validator.TryValidateObject(this, new ValidationContext(this, null, null), new List<ValidationResult>(), true))
return true;
else
return false;
}
private void Save()
{
// Database stuff, maybe WCF stuff, etc.
}
您可以将Button的IsDefault属性绑定到允许的TextBox的IsFocused属性,如下所示。
<TextBox x:Name="TB1" Grid.Row="0" Height="15" Width="300">
</TextBox>
<TextBox x:Name="TB2" Grid.Row="1" Height="15" Width="300">
</TextBox>
<Button Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Height="40" Width="200" Content="Button"
IsDefault="{Binding IsFocused, ElementName=TB2}" Click="Button_Click">
</Button>
我拥有它,因此绑定了IsEnabled属性,但是它阻止了按钮单击的工作。 然后,我尝试基于TB2或Button的焦点对其进行多重绑定,但是如果TB1处于焦点状态,那仍然会阻止单击,因为禁用的按钮无法接受单击来获得焦点。
只需在要处理的文本框上设置AcceptsReturn="True"
即可自行输入,而不是路由到表单。
您可以从按钮中删除IsDefault =“ True”并将命令绑定到文本框本身。 在StackOverflow上有多种方法可以执行此操作。 我的偏好是使用自定义文本框。
public class CommandTextBox : TextBox, ICommandSource
{
private bool _canExecute;
private EventHandler _canExecuteChanged;
protected override bool IsEnabledCore
{
get
{
if (Command != null)
return base.IsEnabledCore && _canExecute;
return base.IsEnabledCore;
}
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTextBox), new PropertyMetadata(OnCommandChanged));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(CommandTextBox));
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTextBox));
public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.Enter)
{
if (Command != null)
{
RoutedCommand command = Command as RoutedCommand;
if (command != null)
command.Execute(CommandParameter, CommandTarget);
else
Command.Execute(CommandParameter);
}
e.Handled = true;
}
}
private void AddCommand(ICommand command)
{
var handler = new EventHandler(CanExecuteChanged);
_canExecuteChanged = handler;
if (command != null)
command.CanExecuteChanged += _canExecuteChanged;
}
private void CanExecuteChanged(object sender, EventArgs e)
{
if (Command != null)
{
RoutedCommand command = Command as RoutedCommand;
// If a RoutedCommand.
if (command != null)
_canExecute = command.CanExecute(CommandParameter, CommandTarget);
else
_canExecute = Command.CanExecute(CommandParameter);
}
CoerceValue(UIElement.IsEnabledProperty);
}
private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
{
// If oldCommand is not null, then we need to remove the handlers.
if (oldCommand != null)
RemoveCommand(oldCommand);
AddCommand(newCommand);
}
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((CommandTextBox)d).HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
}
private void RemoveCommand(ICommand command)
{
EventHandler handler = CanExecuteChanged;
command.CanExecuteChanged -= handler;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.