WPF学习三(MVVM+自定义按钮等的登录界面)

本文介绍了作者通过观看视频学习如何使用WPF创建登录界面,包括布局设计、前后绑定到MVVM模式的应用,以及如何实现控件绑定和解耦。
摘要由CSDN通过智能技术生成

         跟着bilibil龙马哥视频做的一个登录界面,个人感觉讲得很到位,适合新手),他是从开始的前后绑定慢慢解耦合到MVVM,让我较快的理解了WPF的基础。

        

【WPF入门】WPF零基础到精通,从概念到实操,步步提升!_哔哩哔哩_bilibili

成果展示

         

鼠标放置颜色改变

对passwordbox添加属性,能够binding

实现过程

        界面布局,直接使用代码进行编辑

        整体分为上下两部分,及两行,顶行进行标题设计,第二行进行其他控件的设计。

        第二行分为左右两部分,左边这部分放置图片,右边放置账号、密登录按钮这写text和button。下面的代码是最终版本,里面有些控件和属性属于自定义。

    <Grid>
        <!--定义两行-->
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>


        <!--第一行 标题-->
        <TextBlock Text="在线办公管理系统" Width="800" FontWeight="Bold"  Background="#0078d4"
                   FontSize="20" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>

        <!--第二行-->
        <Grid Grid.Row="1">
            <!--分成两列-->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="180"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <!--第一列:图片-->
            <Border Background="LightBlue">
                <Image Source="Images/Login.png" Height="90" Width="90" ></Image>
            </Border>

            <!--第二列 使用border给整个部分赋值背景-->
            <Border Grid.Column="1"  Background="LightCyan">
                <Grid HorizontalAlignment="Center" Height="200" Width="200" VerticalAlignment="Center">
                    <!--三行两列-->
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"></RowDefinition>
                        <RowDefinition Height="30"></RowDefinition>
                        <!--auto 这一行占多少就有多高-->
                        <RowDefinition Height="auto"></RowDefinition>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"></ColumnDefinition>
                        <ColumnDefinition Width="100"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <!--第一行第一列-->
                    <TextBlock Text="账号" Margin="0,0,0,4"></TextBlock>

                    <!--第一行第二列-->
                    <!--Binding 绑定获取文本框的内容-->
                    <!--<TextBox Grid.Column="1" Margin="0,4"   x:Name="txtAccount" ></TextBox>-->
                    <TextBox Grid.Column="1" Margin="0,4"  Text="{Binding AccountModel.Account}"  ></TextBox>
                    <!--第二行第一列-->
                    <TextBlock Text="密码"  Grid.Row="1" Margin="0,4"  ></TextBlock>
                    <!--第二行第二列-->
                    <!--<TextBox Grid.Row="1" Grid.Column="1" Margin="0,4"  Text="{Binding AccountModel.Code}"></TextBox>-->
                    <!--<PasswordBox Grid.Row="1" Grid.Column="1" Margin="0,4"  Password="{Binding AccountModel.Code}"></PasswordBox> 报错 password不是依赖属性-->
                    <PasswordBox Grid.Row="1" Grid.Column="1" Margin="0,4"  pwdns:PasswordBoxExtend.MyPwd="{Binding AccountModel.Code,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged
                        }" pwdns:PasswordBoxExtend.IsBind="True"  ></PasswordBox>
                    <!--第三行 2列合并:Grid.ColumnSpan="2"-->
                    <custBtnNs:CustomButtons OverBackground="green"  BtnRadius="5" Content="登录" Grid.Row="2" Grid.ColumnSpan="2" FontSize="20" Background="LightBlue" 
                            Command="{Binding Command}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="30" Width="100"></custBtnNs:CustomButtons>

                    <!--绑定命令-->

                </Grid>
            </Border>

        </Grid>
    </Grid>

        我这里也按照我的理解一步一步的推进

         2.前后端直接绑定

         最开始实现的是使用的按钮的click和对账号和密码的两个TextBox设置名字,并在后台调用text的方法。

        3.双向绑定

   

        上图的视图View就是主界面,此外设计了一个模型Model与其进行双向绑定 ,账号和密码属性便是定义在model下的后台代码下,这是一个解耦的过程,目的可以实现项目的并行开发。

        4.委托和指令

         

         5.MVVM

        模型处理数据给试图模型,视图模型和界面相互绑定处理。

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的WPF MVVM模式的登录界面及代码: XAML代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" Title="Login" Height="250" Width="350"> <Window.DataContext> <local:LoginViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="Username:"/> <TextBox Grid.Row="0" Grid.Column="1" Width="200" Margin="5" Text="{Binding Username}" /> <Label Grid.Row="1" Grid.Column="0" Content="Password:"/> <PasswordBox Grid.Row="1" Grid.Column="1" Width="200" Margin="5" Password="{Binding Password}" /> <Button Grid.Row="2" Grid.Column="1" Width="100" Margin="5" Content="Login" Command="{Binding LoginCommand}" IsDefault="True"/> <Label Grid.Row="3" Grid.Column="1" Content="{Binding ErrorMessage}" Foreground="Red" HorizontalAlignment="Center"/> <CheckBox Grid.Row="4" Grid.Column="1" Content="Remember Me" Margin="5" IsChecked="{Binding RememberMe}"/> </Grid> </Window> ``` ViewModel代码: ```csharp using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; namespace WpfApp1 { public class LoginViewModel : INotifyPropertyChanged { private string _username; private string _password; private bool _rememberMe; private string _errorMessage; public string Username { get { return _username; } set { _username = value; OnPropertyChanged(); } } public string Password { get { return _password; } set { _password = value; OnPropertyChanged(); } } public bool RememberMe { get { return _rememberMe; } set { _rememberMe = value; OnPropertyChanged(); } } public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } } public ICommand LoginCommand { get; set; } public LoginViewModel() { LoginCommand = new RelayCommand(Login, CanLogin); } private bool CanLogin() { return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password); } private void Login() { if (Username == "admin" && Password == "password") { if (RememberMe) { // Save login information to file or database } MessageBox.Show("Login successful!"); } else { ErrorMessage = "Invalid username or password"; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } ``` 其中,`RelayCommand`是一个实现了`ICommand`接口的自定义命令类,用于绑定按的点击事件。你可以在网上找到相关实现代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值