Wpf基礎之數據綁定

 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.單擊按鈕之後



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章