Wpf中实现属性与控件的双向绑定

最近在学习Wpf,学到了Wpf中最重要的双向绑定,在这做个笔记

 要想实现控件和后台代码的双向绑定,就必须继承INotifyPropertyChanged这个接口,并且实现PropertyChangedEventHandler这个事件,话不多说,上代码:

这个是后台代码

public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        //public string UserName { get; set; }
        //public string PassWord { get; set; }
        //双向绑定
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private string usename;

        public string UserName
        {
            get { return usename; }
            set { usename = value;RaisePropertyChanged("UserName"); }
        }
        private string password;

        public string PassWord
        {
            get { return password; }
            set { password = value;RaisePropertyChanged("PassWord"); }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //string userName = txtUserName.Text;
            //string passWord = txtPassword.Text;

            if (UserName == "Wpf" && PassWord == "666")
            {
                index index = new index();
                index.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("账号密码错误", "警告");
                UserName = "";
                PassWord = "";
            }
        }
    }

 这个是前端xaml代码

<Window x:Class="Wpf_loginUi.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf_loginUi"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="9*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            
        </Grid.ColumnDefinitions>
        <TextBlock Margin="5" Grid.Row="0" Grid.Column="0" Text="宝安区-区图书馆系统" FontSize="18" Foreground="Black" HorizontalAlignment="Center"/>
        <StackPanel Grid.Row="1" Grid.Column="0" Background="#0078d4">
            <TextBlock Margin="5" Text="登录"  FontSize="22" HorizontalAlignment="Center" Foreground="White"/>
        </StackPanel>
        <Grid Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="用户名" Grid.Row="0" Grid.Column="0"/>
            <TextBox Grid.Row="0" Text="{Binding UserName}"  Grid.Column="1" Margin="2"/>
            <TextBlock Text="密码" Grid.Row="1" Grid.Column="0"/>
            <TextBox Grid.Row="1" Text="{Binding PassWord}"  Grid.Column="1" Margin="2"/>

            <CheckBox Content="记住密码" Grid.Row="2" Grid.ColumnSpan="2"/>
            <Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2" Click="Button_Click"/>

        </Grid>

    </Grid>
</Window>

我也是刚开始学习wpf,有什么不足请各位多多指教!!

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值