John Vasquez
John Vasquez

Reputation: 115

How to add Enter Button logic to textbox

I have the following WPF View where the user enters information for three fields, in order to save them, then then press the 'OK' Button. What is the best approach to add logic where the user can also hit enter on any of the text-boxes instead and by pressing enter, it binds to my OKButton Command / logic?

XAML.CS

namespace Pandai.Application.Framework.View
{
    public partial class PopupLockDatabaseView : Window
    {
        public PopupLockDatabaseViewModel model;
        public PopupLockDatabaseView()
        {
            InitializeComponent();
            this.Loaded += OnClose;
            txtFor.Focus();
        }



        void OnClose(object sender, RoutedEventArgs e)
        {
            model = (PopupLockDatabaseViewModel)this.DataContext;
            model.View = this;

        }

    }
}

View + Picture of View enter image description here

                <TextBlock VerticalAlignment="Center" Margin="5,9,5,8" Grid.Column="0" Grid.Row="1" Text="Locked Out By:"/>
                <TextBox  x:Name="txtBy" Grid.Column="1" Grid.Row="1" Text="{Binding LockedOutBy, UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="2"/>

                <TextBlock VerticalAlignment="Center" Margin="5,9,5,8" Grid.Column="0" Grid.Row="2" Text="Locked Out For:"/>
                <TextBox x:Name="txtFor" Grid.Column="1" Grid.Row="2" Text="{Binding LockedOutFor, UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="2"/>

                <TextBlock VerticalAlignment="Center" Margin="5,9,5,8" Grid.Column="0" Grid.Row="3" Text="Locked Out Date:"/>
                <DatePicker x:Name ="picker1"  Grid.Column="1" Grid.Row="3" SelectedDate="{Binding LockedOutDate, UpdateSourceTrigger=PropertyChanged}" SelectedDateFormat="Short" Grid.ColumnSpan="2"/>




            </Grid>

            <Border Grid.Column="0" Margin="5"
                    Grid.Row="2">
                <WrapPanel HorizontalAlignment="Right">

                    <Button x:Name="btnOK" Command="{Binding Path=OKCommand}"
                                 Content="_OK" Margin="4,2"  MinWidth="60"/>

                    <Button x:Name="btnCancel" Command="{Binding Path=CancelCommand}"
                                 Content="_Cancel" Margin="4,2"  MinWidth="60"/>
                </WrapPanel>
            </Border>

VIEWMODEL OK-BUTTON LOGIC

 public ICommand OKCommand
        {
            get { return new RelayCommand(c => OnOKLock()); }
        }


            protected void OnOKLock()
        {
            var currentSetting = AppSession.Repository.Settings.Find(SettingQuery.ID == new ID("LockedOutDate"));
            currentSetting[0].Value = LockedOutDate; 
            AppSession.Repository.Settings.Save(currentSetting[0]);

            currentSetting = AppSession.Repository.Settings.Find(SettingQuery.ID == new ID("LockedOutBy"));
            currentSetting[0].Value = LockedOutBy;
            AppSession.Repository.Settings.Save(currentSetting[0]);

            currentSetting = AppSession.Repository.Settings.Find(SettingQuery.ID == new ID("LockedOutFor"));
            currentSetting[0].Value = LockedOutFor;
            AppSession.Repository.Settings.Save(currentSetting[0]);

            currentSetting = AppSession.Repository.Settings.Find(SettingQuery.ID == new ID("IsUsersLockedOut"));
            currentSetting[0].Value = "1"; 
            AppSession.Repository.Settings.Save(currentSetting[0]);

            //MessageBox.Show("Please Logout and log back in.");

            View.Close();

        }

Upvotes: 0

Views: 58

Answers (1)

AJITH
AJITH

Reputation: 1175

You can set the IsDefault property of the button to True for making that as an Accept button.

Upvotes: 1

Related Questions