按Enter键

时间:2016-11-18 14:26:20

标签: c# wpf xaml mvvm

我有一个WPF XAML屏幕,有多个控件(文本框,组合框,按钮等)。背后的代码是在C#中。我为其中一个按钮保留了IsDefault="True"。因此,如果用户在任何文本框中按 Enter 键,表单将被提交。

我只需要为一个特定的文本框使用 Enter 键提交表单。如果用户在任何其他文本框中按 Enter 键,我不希望提交表单。

我知道我可以使用后面的代码(即*.xaml.cs)来实现这一点。但是如何使用MVVM设计模式实现这一目标呢?

4 个答案:

答案 0 :(得分:2)

您可能不关心用户按下哪个文本框,您只需要完成整个表单。如果是这种情况,请执行验证并使用绑定禁用该按钮。

Dim source As Range, col As Range, cell As Range
Dim res As Double

Set source = Selection

With source
    For Each col In .Columns
        For Each cell In col.Cells
            If cell.Font.Bold Then
                cell.Value = res
                res = 0
            Else
                res = res + cell.Value
            End If
        Next cell
    Next col
End With

答案 1 :(得分:2)

您可以将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被关注,仍会阻止点击,因为禁用的按钮无法接受点击以获得焦点。

答案 2 :(得分:1)

只需在您要处理的文本框中设置AcceptsReturn="True"即可自行输入,而不是路由到表单。

答案 3 :(得分:0)

你可以删除IsDefault =&#34; True&#34;从您的按钮,并将您的命令绑定到文本框本身。在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;
    }
}
相关问题