WPF中利用控件的DataContext属性为多个TextBox绑定数据
2021-06-19 22:07
标签:turn 方法 分享图片 姓名 xaml end prope def bin 工作上需要从给定的接口获取数据,然后显示在界面的编辑框中,以往肯定会一个一个的去赋值,但这样太麻烦而且效率很低,不利于维护,于是想到了数据绑定这一方法,数据绑定主要利用INotifyPropertyChanged这一接口去监听属性是否发生改变。下面是我写的一个demo,主要是利用控件的DataContext属性绑定数据 1.数据源 2.界面布局 3.实例操作 4.效果显示 WPF中利用控件的DataContext属性为多个TextBox绑定数据 标签:turn 方法 分享图片 姓名 xaml end prope def bin 原文地址:https://www.cnblogs.com/QingYiShouJiuRen/p/10272385.htmlusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBinding
{
public class DataSource:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
///
Window x:Class="DataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="主窗口" Height="350" Width="525" WindowStartupLocation="CenterScreen">
Grid x:Name="grid_DataInfo">
Grid.ColumnDefinitions>
ColumnDefinition Width="20"/>
ColumnDefinition Width="*"/>
ColumnDefinition Width="20"/>
Grid.ColumnDefinitions>
Grid.RowDefinitions>
RowDefinition Height="20"/>
RowDefinition Height="*"/>
RowDefinition Height="20"/>
Grid.RowDefinitions>
StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Margin="130,30,0,0">
TextBlock Text="姓名:" Margin="0,3,0,0"/>
TextBox x:Name="txt_Name" Text="{Binding Path=Name}" FontSize="16" VerticalContentAlignment="Center" Width="150"/>
StackPanel>
StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Margin="130,80,0,0">
TextBlock Text="年龄:" Margin="0,3,0,0"/>
TextBox x:Name="txt_Age" Text="{Binding Path=Age}" FontSize="16" VerticalContentAlignment="Center" Width="150"/>
StackPanel>
StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Margin="130,130,0,0">
TextBlock Text="性别:" Margin="0,3,0,0"/>
TextBox x:Name="txt_Gender" Text="{Binding Path=Gender}" FontSize="16" VerticalContentAlignment="Center" Width="150"/>
StackPanel>
StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Margin="130,180,0,0">
TextBlock Text="身高:" Margin="0,3,0,0"/>
TextBox x:Name="txt_Height" Text="{Binding Path=Height}" FontSize="16" VerticalContentAlignment="Center" Width="150"/>
StackPanel>
Grid>
Window>
namespace DataBinding
{
///
文章标题:WPF中利用控件的DataContext属性为多个TextBox绑定数据
文章链接:http://soscw.com/index.php/essay/96145.html