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

 收藏

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

XAML文件

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>

视图模型OK按钮逻辑

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

        }
回复
  • ket 回复

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