工作上需要从给定的接口获取数据,然后显示在界面的编辑框中,以往肯定会一个一个的去赋值,但这样太麻烦而且效率很低,不利于维护,于是想到了数据绑定这一方法,数据绑定主要利用INotifyPropertyChanged这一接口去监听属性是否发生改变。下面是我写的一个demo,主要是利用控件的DataContext属性绑定数据

1.数据源

using 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; /// <summary>
/// 姓名
/// </summary>
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
} /// <summary>
/// 年龄
/// </summary>
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
} /// <summary>
/// 性别
/// </summary>
private string _gender;
public string Gender
{
get { return _gender; }
set
{
_gender = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Gender"));
}
}
} /// <summary>
/// 身高
/// </summary>
private int _height;
public int Height
{
get { return _height; }
set
{
_height = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Gender"));
}
}
} private static DataSource _instance = null;
public static DataSource GetInstance()
{
if(null == _instance)
{
_instance = new DataSource();
} return _instance;
} private DataSource()
{
_name = "张三";
_age = ;
_gender = "男";
_height = ;
} }
}

2.界面布局

<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>

3.实例操作

namespace DataBinding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); //绑定数据源
grid_DataInfo.DataContext = DataSource.GetInstance();
}
}
}

4.效果显示

WPF中利用控件的DataContext属性为多个TextBox绑定数据的更多相关文章

  1. WPF中PasswordBox控件的Password属性的数据绑定

    原文:WPF中PasswordBox控件的Password属性的数据绑定 英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://bl ...

  2. WPF中Image控件的Source属性

    原文:WPF中Image控件的Source属性 imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性.我先如下方式进行: Uri uri = new Uri(strImag ...

  3. 【转】WPF中PasswordBox控件的Password属性的数据绑定

    英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://blog.csdn.net/oyi319/article/details/65 ...

  4. WPF中DataGrid控件内Button的Command和CommandParameter的绑定

    场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...

  5. WPF中Image控件的Source属性的设置

    1.直接关联到文件,关联后不能删除此图片,因为图片正在使用. imageEditImage.Source = new BitmapImage(new Uri(strImagePath, UriKind ...

  6. WPF中PasswordBox控件无法绑定Password属性解决办法

    在WPF中,默认的Password控件的Password属性是不允许为之绑定的,下面是一个解决绑定Password的方法的代码: 1.前台代码 <Window x:Class="Pas ...

  7. WPF中TreeView控件数据绑定和后台动态添加数据(二)

    写在前面:在(一)中,介绍了TreeView控件MVVM模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...

  8. WPF中TreeView控件数据绑定和后台动态添加数据(一)

    数据绑定: 更新内容:补充在MVVM模式上的TreeView控件数据绑定的代码. xaml代码: <TreeView Name="syntaxTree" ItemsSourc ...

  9. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

随机推荐

  1. 第一次AOP,附上使用DEMO,可用在简单生产环境了

    demo代码如下 public class ConsoleTimeAttribute : ApectBaseAttribute { public override void Before(ApectC ...

  2. jQuery中关于全选、全不选和反选

    1.首先我们要获取当前点击的对象,然后得到点击事件,   判断他的状态如果是checked的话就把该第二行的选中,   否则就取消选中. 2.当第二列功能小项没有全部选中时,该行第一列的复选款也要取消 ...

  3. JavaScript是如何工作的:深入类和继承内部原理 + Babel和TypeScript 之间转换

    这是专门探索 JavaScript 及其所构建的组件的系列文章的第 15 篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工作的:引擎,运行时和调用堆栈的概述! Jav ...

  4. noi.ac #289. 电梯(单调队列)

    题意 题目链接 Sol 傻叉的我以为给出的\(t\)是单调递增的,然后\(100\rightarrow0\) 首先可以按\(t\)排序,那么转移方程为 \(f[i] = min_{j=0}^{i-1} ...

  5. SAP MM 启用批次管理的物料MB21创建预留单据时批次号可以为空!

    SAP MM 启用批次管理的物料MB21创建预留单据时批次号可以为空! 如下预留, 批次号字段为空. 实际上该物料是有激活batch management的, For MB21, it is just ...

  6. JVM内存管理 《深入分析java web 技术内幕》第八章

    8.1 物理内存与虚拟内存 物理内存RAM(随机存储器),寄存单元为寄存器,用于存储计算单元执行指令的中间结果. 连接处理器和RAM或者处理器和寄存器的是地址总线,这个地址的宽度影响了物理地址的索引范 ...

  7. sql 语句-初级进阶(二)

    1.insert插入数据时注意事项: 每次插入一整行,不能半行或者是几列数据. 值与列数必须相同,包括数据类型也必须匹配. 不能为标识列指定值(就是最前面的那一序号列),它是自动增长的 列的数据类型为 ...

  8. 20181218-PostgreSQL数据库Extension管理

    20181218-PostgreSQL数据库Extension管理 注意:在集群的一个数据库中安装扩展,在集群的另一个数据库要使用的话,仍需安装 1. 查看当前已安装Extension postgre ...

  9. C# List集合去重使用lambda表达式

    name age sex Lucy 22 woman Lily 23 woman Tom 24 man Lucy 22 woman Lily 23 woman LiLei 25 man List< ...

  10. Python爬虫之正则表达式(1)

    廖雪峰正则表达式学习笔记 1:用\d可以匹配一个数字:用\w可以匹配一个字母或数字: '00\d' 可以匹配‘007’,但是无法匹配‘00A’; ‘\d\d\d’可以匹配‘010’: ‘\w\w\d’ ...