工作上需要从给定的接口获取数据,然后显示在界面的编辑框中,以往肯定会一个一个的去赋值,但这样太麻烦而且效率很低,不利于维护,于是想到了数据绑定这一方法,数据绑定主要利用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. Linux,在不使用U盘的情况下使用wubi.exe程序在Win7上安装ubuntu-14.04.3版系统

    本文介绍如何在不使用U盘的情况下使用wubi.exe程序在Win7上安装ubuntu-14.04.3版系统. 花了一天的时间终于安装上了Ubuntu14.04,过程坎坷,是血泪史,开始报“cannot ...

  2. Android项目刮刮奖详解(一)

    前言 最近正在学鸿洋大大的刮刮奖,感觉学有所得,便是来写篇详解(尽管网上有很多了,不过毕竟是自己写的,自己以后方便复习),正文开始 目标 实现画板功能 思路 我们需要自定义View来实现画板功能,之后 ...

  3. Java 学习笔记 使用synchronized实现生产者消费者模式

    说明 Object.wait()使当前的线程进入到等待状态(进入到等待队列) Object.notifyAll() 唤醒等待中的全部线程 Object.notify() 随机唤醒一个线程 代码 con ...

  4. [leetcode](4.21)4. 有效子数组的数目

    给定一个整数数组 A,返回满足下面条件的 非空.连续 子数组的数目: 子数组中,最左侧的元素不大于其他元素. 示例 1: 输入:[1,4,2,5,3] 输出:11 解释:有 11 个有效子数组,分别是 ...

  5. 【阿里云】在 Windows Server 2016 下使用 FileZilla Server 安装搭建 FTP 服务

     Windows Server 2016 下使用 FileZilla Server 安装搭建 FTP 服务 一.安装 Filezilla Server 下载最新版本的 Filezilla Server ...

  6. Lansat8大气校正USGS-EROS项目espa-surface-reflectance中的LaSRC Version 1.3.0模块利用vs2010编译出windows64位版本的使用(三)

    Landsat8大气校正程序LaSRC是目前最好的,使用方式也够傻瓜,输入文件输出结果. 但有一个限制,就是程序需要预先下载好的MODIS辅助文件来确定水汽.压强等大气参数. 如果待大气校正的land ...

  7. android中的相对路径

    转载请标明出处:https://www.cnblogs.com/tangZH/p/9939655.html  1.同个文件夹访问 D:\Java\main\A.java D:\Java\main\B. ...

  8. Android开发中碰到的一个ANR问题。

    这是一个点击之后反应超时的ANR [// ::] - :: D ActivityManager: Delay resumeKeyDispatchingLocked() to avoid deadloc ...

  9. kmalloc分配物理内存与高端内存映射--Linux内存管理(十八)

    1 前景回顾 1.1 内核映射区 尽管vmalloc函数族可用于从高端内存域向内核映射页帧(这些在内核空间中通常是无法直接看到的), 但这并不是这些函数的实际用途. 重要的是强调以下事实 : 内核提供 ...

  10. 能ping通虚拟机,但snmp报文 Destination unreachable(Host administratively prohibited

    如题目,使用virtual box 虚拟机,虚拟机系统为centos6.5, 主机系统为win10 内外设置ip在同一网段后,互相能ping通,centos 系统开启snmp服务,此处说明以下, sn ...