登录简单的命令使用

//登录简单的命令使用

      <!--Grid网格布局-->

            <Grid x:Name="grdLogin">

                <Grid.Background>

                    <!--渐变画刷:使用线性渐变绘制区域。线性渐变沿直线定义渐变-->

                    <LinearGradientBrush  StartPoint="0,0" 

                                          EndPoint="0,1">

                        <!--使用LinearGradientBrush渐变画刷:定义一个渐变画刷,然后需要定义渐变颜色,颜色已直线形式渐变-->

                        <!--GradientStop:定义渐变颜色与颜色渐变始点-->

                        <GradientStop Color="#FF1E48F1" 

                                      Offset="0"/>

                        <GradientStop Color="#FFF5F6F9" 

                                      Offset="1"/>

                    </LinearGradientBrush>

                </Grid.Background>

                <!--HorizontalAlignment:水平方向对齐方式 -->

                <!--VerticalAlignment:垂直方向对齐方式-->

                <TextBlock  

                    Name="tbZhangHao"

                    Text="账号:" 

                            Height="28" 

                            Margin="345,89,0,0" 

                            HorizontalAlignment="Left"  

                            VerticalAlignment="Top"  

                            FontSize="12" RenderTransformOrigin="0.5,0.5">

                    <TextBlock.RenderTransform>

                        <TransformGroup>

                            <ScaleTransform/>

                            <SkewTransform/>

                            <RotateTransform/>

                            <TranslateTransform/>

                        </TransformGroup>

                    </TextBlock.RenderTransform>

                </TextBlock>

                <!--MaxLength属性:输入值的最大长度,不包含硬编码中Text的字符长度-->

                <!--TextWrapping:如果设置成wrap ,就表示当文本长度超过容器长度时可以自动换行。

                                    默认为no wrap,即当文本长度超过容器长度时,文本超出部分被遮挡。-->

                <!--AcceptsReturn:允许回车换行操作-->

                <TextBox 

                            Name="txt_ZhangHao"

                            Text="001"

                            MaxLength="20"  

                            TextWrapping="NoWrap"   

                            Height="23" Width="120"

                            HorizontalAlignment="Left" 

                            VerticalAlignment="Top"  

                            Margin="400,86,0,0" 

                            AcceptsReturn="True">

 

                </TextBox>

 

                <TextBlock 

                    Name="tbMiMa"

                    Text="密码:" 

                           Height="23"  

                           FontSize="12" 

                           Margin="345,120,0,0"

                           HorizontalAlignment="Left"

                           VerticalAlignment="Top" />

                <!--Password属性:与TextBox的Text属性一样-->

                <PasswordBox 

                            Name="pwd_MiMa"

                            Password="123"   

                            Height="23" 

                            Width="120"

                            HorizontalAlignment="Left" 

                            VerticalAlignment="Top" 

                            Margin="400,119,0,0" />

                <!--Content:作用与TextBox的Text属性一样,但其数据类型为object,即可放任何对象,但只能存放一个对象-->

                <Button  

                    Name="btn_Login" 

                    Content="登陆" 

                    Height="23"

                    Width="75" 

                    Background="SlateBlue" 

                    HorizontalAlignment="Left"

                    VerticalAlignment="Top" 

                    Margin="362,162,0,0"/>

                <!--Cursor:当光标进入控件范围时使用的鼠标指针-->

                <Button  

                    Name="btn_QuXiao" 

                    Content="取消" 

                    Height="23" 

                    Width="75" 

                    Background="SlateBlue" 

                    HorizontalAlignment="Left" 

                    VerticalAlignment="Top" 

                    Margin="445,162,0,0" 

                    Cursor="Help" 

                    Click="btn_QuXiao_Click">

                </Button>

            </Grid>

 

1、声明并定义命令

  private RoutedCommand LoginCmd = new RoutedCommand("Login", typeof(Login));

//初始化命令的方法

private void InitializeCommand()

{

   //把命令赋值给命令源

   this.btn_Login.Command=this.LoginCmd;

   //创建命令关联

    CommandBinding cb = new CommandBinding();

  //指定关联的命令

     cb.Command = this.LoginCmd;

  //确定此命令是否可以在其当前状态下执行

     cb.CanExecute += cb_CanExecute;

   //调用此命令

    cb.Executed += cb_Executed;

  //把命令关联在外围控件上

     this.grdLogin.CommandBindings.Add(cb);

}

//执行命令,要做的事情

viod  cb_Executed(object sender, ExecutedRoutedEventArgs e)

{

    //获取用户填写的用户名和密码

            string YongHuMing = txt_ZhangHao.Text.ToString().Trim();

            string pwd = pwd_MiMa.Password.ToString().Trim();

            //使用Linq,查询用户名和密码是否正确

            var qpEmp = from emp in MF.BM_用户表

                        where emp.账号 == YongHuMing.Trim() && emp.密码 == pwd.Trim()

                        select emp;

            if (qpEmp.Count() > 0)

            {

                //用户名和密码正确

                //获取登陆成功用户的员工ID与员工姓名,并存储在静态变量中

                //dctDengLu.EMP_员工表  表示查询的集合,emp可随意命名,表示集合中的元素

                var qpEmpMsg = (from emp in MF.BM_用户表

                                where emp.账号 == YongHuMing.Trim() && emp.密码 == pwd.Trim()

                                select emp).Single();

                //打开主窗体

                MainWindow myMain = new MainWindow();

                // Main myMain = new Main();

                myMain.Show();

                this.Close();

            }

            else

            {

                MessageBox.Show("用户名或密码错误");

                this.pwd_MiMa.Clear();

            }}

 

//在执行命令之前,检查命令是否可以执行对应的处理器

        void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)

        {

            if (string.IsNullOrEmpty(this.txt_ZhangHao.Text) || string.IsNullOrEmpty(this.pwd_MiMa.Password))

            {

 

                e.CanExecute = false;

            }

            else

            {s

                e.CanExecute = true;

            }

        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值