先做个声明:这里绑定都在前台实现,至于后台怎么写,那比前台简单多了,但更常用的是xaml中绑定。我们分析下最简单的字符串绑定来弄清楚原理,其他的类推就是。

数据绑定主要是要弄清楚两个东西,一个是源Source,一个是路径Path。

什么能够作为源Source呢:

CLR类型的单个对象

CLR类型的集合对象

DataTable和DataView

XML数据

依赖对象

FrameworkElement的DataContext

控件及ElementName

假如现在有一个最简单的需求:有一个窗口MainWindow,它里面有一个文本框(TextBox),Name为textBox,文本框的内容(Text)要绑定一个字符串。

好了,现在我们要考虑一个上面需求没提到的问题,该字符串是哪的字符串?

1.另外一个类Student(含公开属性Name)的实例化对象对应的Name

在哪实例化这个类Student呢?

  • 在MainWindow.xaml.cs里,对不起,即使实例化了,xaml里无法直接访问cs里的实例化的对象。
  • 在xaml里实例化,有个不错的选择就是通过声明资源(Resource)
 <Grid>
<Grid.Resources>
<local:Student x:Key="stu" StuName="张三"/>
</Grid.Resources>
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding Source={StaticResource stu},Path=StuName}"/>
</Grid>

这种方法测试是可行的。

  • 那如果通过数据上下文DataContext的方式呢,怎么写

DataContext只要存在于要绑定的控件的本身或者其逻辑树父级以上就可以,我们还是定义在Grid上吧

 <Grid Name="grid">
<Grid.DataContext>
<local:Student StuName="李四"/>
</Grid.DataContext>
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding Path=StuName}"/>
</Grid>

这种方法测试也是可行的。

2.如果该字符串存在于当前页面,是当前页面的一个公开属性呢

 public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private string str; public string Str
{
get
{
return str;
} set
{
str = value;
}
}
}
  • 我们先尝试用资源实例化
 <Window.Resources>
<local:MainWindow x:Key="str" Str="王五" />
</Window.Resources>
<Grid Name="grid">
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding Source={StaticResource str}, Path=Str}" />
</Grid>

启动的时候报错了:

原因是资源不允许嵌套,就是说页面定义的资源不能是本身的实例。

  • 再试试DataContext
 <Grid Name="grid">
<Grid.DataContext>
<local:MainWindow Str="王五" />
</Grid.DataContext>
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding Path=Str}" />
</Grid>

跟上面报了相同的错,看来数据上下文也不能指定为自己的实例。

  • 那就没招了吗?有的,直接将整个窗口(它也是FrameworkElement)作为Source,通过指定ElementName的方式
 <Window x:Class="Binding.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:local="clr-namespace:Binding"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d"
Name="this">
<Grid Name="grid">
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding ElementName=this, Path=Str}" />
</Grid>
</Window>

我们需要在cs里对Str赋值,否则绑定的是Str的默认值,这块我们这样理解,ElementName指定自己,那么就是实例化了一个MainWindow,它里面的属性都是取的默认值。

我们在构造里写 Str=“ZHAOLIU”,发现运行的文本框仍然为空,这是因为我们赋值是在界面初始化语句InitializeComponent();之后,

我们的属性又不具备更改通知功能,所以改变了值也不会起作用。下面我们就改造为可更改通知的。只需要继承接口INotifyPropertyChanged并实现他的约束就可以了。

 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 Binding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
Str = "ZHAOLIU";
}
private string str; public event PropertyChangedEventHandler PropertyChanged; public string Str
{
get
{
return str;
} set
{
str = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Str"));
}
}
}
}
}

经过测试是可以的。

3.如果字符串存在于另一个窗口,是其一个公开属性呢

我们再新建个窗口Window1

 public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private string str; public string Str
{
get
{
return str;
} set
{
str = value;
}
}
}

然后我们在MainWindow里实例化Window1试试

 <Window.Resources>
<local:Window1 x:Key="str" Str="王五" />
</Window.Resources>
<Grid Name="grid">
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding Source={StaticResource str}, Path=Str}" />
</Grid>

这是可以的。至此我们知道,资源中实例化一个对象,控件去绑定该对象的公开属性,只要一条,该对象不能是自己本身就ok。

使用DataContext也是可以的

 <Window.DataContext>
<local:Window1 Str="麻子"/>
</Window.DataContext>
<Grid Name="grid">
<TextBox Name="textBox"
Width="150"
Height="23"
BorderBrush="Black"
Text="{Binding Path=Str}" />
</Grid>

ElementName呢,MainWindow拿不到Window1里的控件,就算你给Window1给了个名字,在MainWindow也不能以字符串直接拿到,不知道你们有没有见过将控件的值绑定到另一个页面的一个控件上的。

总结下:

wpf XAML xaml 进行 数据绑定,Resource DataContext ElementName的更多相关文章

  1. WPF:XAML概述

    简介 XAML是eXtensible Application Markup Language可扩展应用程序标记语言,它是微软公司为构建应用程序用户界面而创建的一种新的描述性语言.XAML提供了一种便于 ...

  2. .Net Core WPF之XAML概述

    原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...

  3. wpf中xaml的类型转换器与标记扩展

    原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...

  4. How can I list colors in WPF with XAML?

    How can I get list of all colors I can pick in Visual Studio Designer (which is System.Windows.Media ...

  5. 【C#】WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的

    原文:[C#]WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的 初学WPF,知道一些控件可以通过定义Style的Trigger改变要显示的样式,但是 ...

  6. 整理:WPF中Xaml中绑定枚举的写法

    原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...

  7. Wpf读写Xaml文件

    前言 本文主要介绍Wpf读写Xaml文件. 读写实现 首先我们使用XamlWriter将Wpf的对象转换为Xaml字符串,代码如下: var btn = sender as Button; strin ...

  8. WPF SDK研究 之 数据绑定

    这一章介绍数据绑定.本章共计27个示例,全都在VS2008下.NET3.5测试通过,点击这里下载:ConnectedData.rar 1.ShowDataWithoutBinding注: <?M ...

  9. WPF笔记(1.6 数据绑定)——Hello,WPF!

    原文:WPF笔记(1.6 数据绑定)--Hello,WPF! 这个一节都是在讲一个数据绑定的示例.功用:输入姓和名,点击Add按钮,ListBox增加一条记录,永远是字符串“name: nick”:L ...

随机推荐

  1. js中将yyyy-MM-dd格式的日期转换

    1.转换为yyyy年MM月dd日 var str = "2017-02-16"; var reg =/(\d{4})\-(\d{2})\-(\d{2})/; var date = ...

  2. redis intset(整数集合)

    redis intset (整数集合) 概述 intset 是集合的底层实现结构之一 intset 集合只包含整数 intset 自升级 intset 整数集合是有序的 intset 结构 结构 // ...

  3. spring 动态创建数据源

    项目需求如下,公司对外提供服务,公司本身有个主库,另外公司会为每个新客户创建一个数据库,客户的数据库地址,用户名,密码,都保存在主数据库中.由于不断有新的客户加入,所以要求,项目根据主数据库中的信息, ...

  4. 关于Java中继承多接口同名方法的问题

    在Java中如果一个类同时继承接口A与B,并且这两个接口中具有同名方法,会怎么样? 动手做实验: interface A{ void fun(); } interface B{ void fun(); ...

  5. 一个 Vue + Node + MongoDB 博客系统

    源码 耗时半载(半个月)的大项目终于完成了.这是一个博客系统,使用 Vue 做前端框架,Node + express 做后端,数据库使用的是 MongoDB.实现了用户注册.用户登录.博客管理(文章的 ...

  6. STM32实战应用(一)——1602蓝牙时钟1液晶的显示测试

    前言 从51到STM32F4学习这么久了,总算找到点头绪了,目前学习了GPIO,中断,定时器,看门狗的基本使用,所以想试着看看能不能做个什么东西,就是想复习一下最近学习的知识.正好上学期单片机课程设计 ...

  7. NodeJS 阻塞/非阻塞

    阻塞实例 var fs = require('fs'); var data = fs.readFileSync('test.txt') console.log(data.toString()); co ...

  8. poj2104 Kth-Number

    Description You are working for Macrohard company in data structures department. After failing your ...

  9. hdu5145 NPY and girls

    人生中第一道莫队,本来以为是一道水题的.. 首先这题只有区间查询,没有修改操作,使用莫队比较明显,但统计答案有点麻烦.. 根据题意,在n个人里选m个不相同种类的人,设第i种人数量为ai,总方案为c(n ...

  10. ASP.NET Cookie和Session

    Cookie和Session C#在服务器,JS在客户端 客户端验证不能代替服务端验证 Http HTTP属于应用层,HTTP 协议一共有五大特点:1.支持客户/服务器模式;2.简单快速;3.灵活;4 ...