WPF -001 数据绑定
发布日期:2021-11-07 06:40:51 浏览次数:12 分类:技术文章

本文共 4199 字,大约阅读时间需要 13 分钟。

    WPF中界面全部都用XAML显示相应的控件,确切来说,WPF是想让WINFORM界面的开发像网页开发一样。在Winform中只要控件名.属性=设置的属性值,那么在WPF中不用再这么麻烦了。这里就说说数据绑定的用法吧。界面上有很多要显示后台数据的控件,后台数据改变了,那么如何让界面数据也实时发生改变呢?这就用到绑定。

使用数据绑定有这么几个步骤:
第一步,有一个实现了INotifyPropertyChanged接口的类;
第二步,页面显示的数据模型类,这个类要继承自第一步的实现了INotifyPropertyChanged接口的类;
第三步,界面的后台类要声明数据模型类,并且要绑定
 this.DataContext = this.数据模型类对象;//绑定,这句才实现了绑定
 第四步,界面控件的相应属性和模型类对象绑定,如下,
 <TextBox  Text="{Binding 数据模型类相应属性}" />
 看代码:
 1、实现接口的类

///     /// NotiyObject必须实现INotifyPropertyChanged接口    ///     public abstract class NotifyProperty : INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void OnChangedProperties(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }    }

2、页面显示的数据模型类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;using System.Windows;namespace TestAttributeDependency.BindData{    public class MainFormDataModel : NotifyProperty    {        private string textBoxContent = string.Empty;        private Visibility btnShowContent = Visibility.Visible;        private Visibility btnClearContent = Visibility.Collapsed;        ///         /// 文本框内显示的内容        ///         public string TextBoxContent        {            get            {                return this.textBoxContent;            }            set            {                this.textBoxContent = value;                //必须调用该事件才能完成前台相应绑定内容的改变                //而且propertyName最好和绑定的对象名称一致                this.OnChangedProperties(@"TextBoxContent");            }        }        ///         /// 显示内容按钮是否显示        ///         public Visibility BtnShowContent        {            get            {                return this.btnShowContent;            }            set            {                this.btnShowContent = value;                this.OnChangedProperties(@"BtnShowContent");            }        }        ///         /// 清空内容按钮是否显示        ///         public Visibility BtnClearContent        {            get             {                 return btnClearContent;            }            set             {                btnClearContent = value;                this.OnChangedProperties(@"BtnClearContent");            }        }    }}

3、界面的后台声明数据模型对象

using System.Windows;using TestAttributeDependency.BindData;namespace TestAttributeDependency{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        ///         /// 需要绑定的对象        ///         internal MainFormDataModel mainFormDataModel = new MainFormDataModel();        ///         /// 构造函数        ///         public MainWindow()        {            InitializeComponent();            this.DataContext = this.mainFormDataModel;//绑定,这句才实现了绑定            InitializeCutom();        }        ///         /// 自定义初始化        ///         void InitializeCutom()        {            mainFormDataModel.TextBoxContent = string.Empty;            mainFormDataModel.BtnShowContent = System.Windows.Visibility.Visible;            mainFormDataModel.BtnClearContent = System.Windows.Visibility.Collapsed;        }        ///         /// 显示内容按钮点击事件        ///         ///         ///         private void btnShowContent_Click(object sender, RoutedEventArgs e)        {            mainFormDataModel.TextBoxContent = @"哈哈,按钮被点击了!";            //通过Visibility控制控件的显示            mainFormDataModel.BtnShowContent = Visibility.Collapsed;            mainFormDataModel.BtnClearContent = Visibility.Visible;        }        ///         /// 清空内容按钮点击事件        ///         ///         ///         private void btnClearContent_Click(object sender, RoutedEventArgs e)        {            if (this.txtShowContent.Text.Length > 0)            {                this.txtShowContent.Text = string.Empty;            }            mainFormDataModel.BtnShowContent = Visibility.Visible;            mainFormDataModel.BtnClearContent = Visibility.Collapsed;        }    }}

4、前台界面的写法

完成后效果如下:

点击“显示内容”按钮,TextBox中显示内容,并且“显示内容”按钮隐藏,"清空内容"按钮显示;点击"清空内容"按钮则相反。

代码下载:

转载地址:https://blog.csdn.net/yysyangyangyangshan/article/details/7846971 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:程序与生活:程序员要保持好奇心
下一篇:C#基础知识整理:基础知识(12) 超类Object

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月24日 12时13分05秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章