WPF 绑定到类成员

程序集整体框架如下

 MainWindow前台代码中Text="{Binding Stu.Name}", Text="{Binding Stu.Gender}" ,Text="{Binding Stu.Age}"都是绑定到类成员

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="GuanJiaoGrid">
        <Grid.DataContext>
            <local:Students></local:Students>
        </Grid.DataContext>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" FontSize="22" >名字</Label>
        <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="2" VerticalAlignment="Center" FontSize="22" >性别</Label>
        <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="3" VerticalAlignment="Center" FontSize="22" >年龄</Label>

        <TextBox  Text="{Binding Stu.Name}"      Grid.Column="1" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox>
        <TextBox  Text="{Binding Stu.Gender}"    Grid.Column="2" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox>
        <TextBox  Text="{Binding Stu.Age}"       Grid.Column="3" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox>
    </Grid>
</Window>
MainWindow.xaml

MainWindow后台代码什么也不写,Students类如下

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApp1
{
    public  class Students
    {
        public Student Stu { get; set; } = new Student();
        public Students()
        {
            Stu.Name   = "李明";
            Stu.Gender = "";
            Stu.Age    = "15";
        }
    }

    #region 学生类
    public class Student : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; NotifyPropertyChanged(); }
        }

        private string _gender;
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; NotifyPropertyChanged(); }
        }

        private string _age;
        public string Age
        {
            get { return _age; }
            set { _age = value; NotifyPropertyChanged(); }
        }

        #region 属性更改
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
    #endregion
}
Students

运行结果如下

 当有多个学生信息的时候,可以绑定到集合类,Students类做一下修改

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApp1
{
    public  class Students
    {
        public ObservableCollection<Student> Stu { get; set; } = new ObservableCollection<Student>();//创建集合类
        public Students()//构造方法添加集合类成员
        {
            Stu.Add(new Student("李明","","15"));
            Stu.Add(new Student("李玟","","14"));
        }
    }

    #region 学生类
    public class Student : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; NotifyPropertyChanged(); }
        }

        private string _gender;
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; NotifyPropertyChanged(); }
        }

        private string _age;
        public string Age
        {
            get { return _age; }
            set { _age = value; NotifyPropertyChanged(); }
        }

        public Student(string name,string gender,string age)
        {
            Name = name;
            Gender = gender;
            Age = age;
        }

        #region 属性更改
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
    #endregion
}
Stutents

MainWindow前台代码中Text="{Binding Stu[0].Name}", Text="{Binding Stu[0].Gender}" ,Text="{Binding Stu[0].Age}"都是绑定到集合类成员

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="GuanJiaoGrid">
        <Grid.DataContext>
            <local:Students></local:Students>
        </Grid.DataContext>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" FontSize="22" >名字</Label>
        <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="2" VerticalAlignment="Center" FontSize="22" >性别</Label>
        <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="3" VerticalAlignment="Center" FontSize="22" >年龄</Label>

        <TextBox  Text="{Binding Stu[0].Name}"      Grid.Column="1" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox>
        <TextBox  Text="{Binding Stu[0].Gender}"    Grid.Column="2" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox>
        <TextBox  Text="{Binding Stu[0].Age}"       Grid.Column="3" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox>

        <TextBox  Text="{Binding Stu[1].Name}"      Grid.Column="1" Grid.Row="2" TextAlignment="Center" FontSize="22" ></TextBox>
        <TextBox  Text="{Binding Stu[1].Gender}"    Grid.Column="2" Grid.Row="2" TextAlignment="Center" FontSize="22" ></TextBox>
        <TextBox  Text="{Binding Stu[1].Age}"       Grid.Column="3" Grid.Row="2" TextAlignment="Center" FontSize="22" ></TextBox>
    </Grid>
</Window>
MainWindow.xaml

 当然收集学生信息这种事情最好是用DataGrid控件去做,这里只是介绍一下前台控件成员如何绑定到类成员以及集合类成员的使用。

原文地址:https://www.cnblogs.com/lizhiqiang0204/p/12777317.html