C# 如何将输入按钮逻辑添加到文本框

C# 如何将输入按钮逻辑添加到文本框,c#,.net,wpf,xaml,mvvm,C#,.net,Wpf,Xaml,Mvvm,我有下面的WPF视图,用户在其中输入三个字段的信息,以便保存它们,然后按下“OK”按钮。添加逻辑的最佳方法是什么?用户也可以在任何文本框上按enter键,然后按enter键,它将绑定到我的OKButton命令/逻辑 XAML.CS namespace Pandai.Application.Framework.View { public partial class PopupLockDatabaseView : Window { public PopupLockDa

我有下面的WPF视图,用户在其中输入三个字段的信息,以便保存它们,然后按下“OK”按钮。添加逻辑的最佳方法是什么?用户也可以在任何文本框上按enter键,然后按enter键,它将绑定到我的OKButton命令/逻辑

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;

        }

    }
}
视图+视图图片

您可以将按钮的属性设置为True,使其成为接受按钮

                <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>
 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();

        }