【问题标题】:How to add Enter Button logic to textbox如何将输入按钮逻辑添加到文本框
【发布时间】:2020-03-27 03:41:29
【问题描述】:

我有以下 WPF 视图,用户在其中输入三个字段的信息,以便保存它们,然后按“确定”按钮。添加逻辑的最佳方法是什么?

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;

        }

    }
}

视图+视图图片

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

        }

【问题讨论】:

    标签: c# .net wpf xaml mvvm


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多