关闭

Wpf基础之数据绑定

标签: c#数据绑定
169人阅读 评论(0) 收藏 举报
分类:

 Wpf绑定自定义数据源(将前端控件内容与后台自定义类数据绑定,单击按钮是可改变前端数据值)


1.xml页面代码,3个TxtBox框,3个Button控件,代码如下:

<Window x:Class="_232.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Margin="109,14,119,0" Name="textBox1" VerticalAlignment="Top" Text="{Binding Path=ID}"/>
        <TextBox Margin="109,54,119,0" Name="textBox2" VerticalAlignment="Top" Text="{Binding Path=Name}"/>
        <TextBox Margin="109,94 119,0" Name="textBox3" VerticalAlignment="Top" Text="{Binding Path=Sex}"/>
        <Button Height="28" Margin="109,144,119,0" Name="Btn1" Content="ID" VerticalAlignment="Top" Click="Btn1_Click"/>
        <Button Height="28" Margin="109 184,119,0" Name="Btn2" Content="Name" VerticalAlignment="Top" Click="Btn2_Click"/>
        <Button Height="28" Margin="109,224,119,0" Name="Btn3" Content="Sex" VerticalAlignment="Top" Click="Btn3_Click"/>
    </Grid>
</Window>

2.后台代码如下,实现一个Student类,绑定数据源,及按钮触发事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _232
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        //public class Student
        //{
        //    public string Name { get; set; }
        //    public string ID { get; set; }
        //    public string Sex { get; set; }
        //}

    
        /// <summary>
        /// 创建类Student
        /// </summary>
        public class Student : INotifyPropertyChanged
        {
            string id;
            /// <summary>
            /// ID号
            /// </summary>
            public string ID
            {
                get
                {
                    return id;
                }
                set
                {
                    if (id != value)
                    {
                        id = value;
                        NotifyPropertyChangedID("ID");
                    }
                }
            }

            string name;
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name 
            {
                get
                {
                    return name;
                }
                set
                {
                    if(value!=name)
                    {
                        name = value;
                        NotifyPropertyChangedName("Name");
                    }
                }
            }

            string sex;
            /// <summary>
            /// 性别
            /// </summary>
            public string Sex
            {
                get
                {
                    return sex;
                }
                set
                {
                    if(value!=sex)
                    {
                        sex = value;
                        NotifyPropertyChangedSex("Sex");
                    }
                }
            }

            /// <summary>
            /// 事件是否触发
            /// </summary>
            public event PropertyChangedEventHandler PropertyChanged;

            /// <summary>
            /// ID号发生改变
            /// </summary>
            /// <param name="info"></param>
            private void NotifyPropertyChangedID(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

            /// <summary>
            /// Name发生改变
            /// </summary>
            /// <param name="info"></param>
            private void NotifyPropertyChangedName(string info)
            {
                if(PropertyChanged!=null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

            /// <summary>
            /// 性别发生改变
            /// </summary>
            /// <param name="infoSex"></param>
            private void NotifyPropertyChangedSex(string infoSex)
            {
                if(PropertyChanged!=null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(infoSex));
                }
            }
        }

        /// <summary>
        /// New 一个对象
        /// </summary>
        Student student;

        public MainWindow()
        {
            InitializeComponent();
            student = new Student();
            student.ID = "1010202";
            student.Name = "张三";
            student.Sex = "男";
            textBox1.DataContext = textBox2.DataContext = textBox3.DataContext = student;
        }

     
        /// <summary>
        /// 单击按钮改变ID号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            student.ID = "1";
        }

        /// <summary>
        /// 单击按钮改变Name
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn2_Click(object sender, RoutedEventArgs e)
        {
            student.Name = "June";
        }

        /// <summary>
        /// 单击按钮改变Sex
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn3_Click(object sender, RoutedEventArgs e)
        {
            student.Sex = "女";
        }
    }
}

测试结果:

1.单击按钮之前


2.单击按钮之后



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:101992次
    • 积分:1654
    • 等级:
    • 排名:千里之外
    • 原创:71篇
    • 转载:0篇
    • 译文:0篇
    • 评论:16条
    博客专栏
    最新评论