wpf XAML xaml 进行 数据绑定,Resource DataContext ElementName
先做个声明:这里绑定都在前台实现,至于后台怎么写,那比前台简单多了,但更常用的是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的更多相关文章
- WPF:XAML概述
简介 XAML是eXtensible Application Markup Language可扩展应用程序标记语言,它是微软公司为构建应用程序用户界面而创建的一种新的描述性语言.XAML提供了一种便于 ...
- .Net Core WPF之XAML概述
原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...
- wpf中xaml的类型转换器与标记扩展
原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...
- 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 ...
- 【C#】WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的
原文:[C#]WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的 初学WPF,知道一些控件可以通过定义Style的Trigger改变要显示的样式,但是 ...
- 整理:WPF中Xaml中绑定枚举的写法
原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...
- Wpf读写Xaml文件
前言 本文主要介绍Wpf读写Xaml文件. 读写实现 首先我们使用XamlWriter将Wpf的对象转换为Xaml字符串,代码如下: var btn = sender as Button; strin ...
- WPF SDK研究 之 数据绑定
这一章介绍数据绑定.本章共计27个示例,全都在VS2008下.NET3.5测试通过,点击这里下载:ConnectedData.rar 1.ShowDataWithoutBinding注: <?M ...
- WPF笔记(1.6 数据绑定)——Hello,WPF!
原文:WPF笔记(1.6 数据绑定)--Hello,WPF! 这个一节都是在讲一个数据绑定的示例.功用:输入姓和名,点击Add按钮,ListBox增加一条记录,永远是字符串“name: nick”:L ...
随机推荐
- Named function expressions demystified
Introduction Surprisingly, a topic of named function expressions doesn't seem to be covered well eno ...
- golang环境
Golang是谷歌开发的一款开源性语言,暂时比较方便的IDE有Inteillj Idea.LiteIDE.Eclipse(Golipse)等,使用起来比较方便的IDE:LiteIDE和Inteillj ...
- 【Java 并发】详解 ThreadPoolExecutor
前言 线程池是并发中一项常用的优化方法,通过对线程复用,减少线程的创建,降低资源消耗,提高程序响应速度.在 Java 中我们一般通过 Exectuors 提供的工厂方法来创建线程池,但是线程池的最终实 ...
- angular 实现导航ng-click切换
angular写的导航.自学angular已有一段时间. <!doctype html><html lang="en"><head> <m ...
- html渲染过程
用户输入url地址,浏览器依据域名寻觅IP地址浏览器向服务器发送http恳求,假如服务器段回来以301之类的重定向,浏览器依据相应头中的location再次发送恳求服务器端承受恳求,处理恳求生成htm ...
- Lambda表达式随笔
1.Lambda表达式是一个匿名函数,其本质其实还是一个函数,因此任何一个Lambda表达式都可以以其它的方式通过普通的函数实现或者代替. 2.Lambda表达式云算符:=>,该运算符读为&qu ...
- Python中使用with语句同时打开多个文件
下午小伙伴问了一个有趣的问题, 怎么用 Python 的 with 语句同时打开多个文件? 首先, Python 本身是支持同时在 with 中打开多个文件的 with open('a.txt', ' ...
- WF4.0以上使用代码完整自定义动态生成执行工作流Xaml文件
给大家分享一下,如何完全使用代码自定义的创建生成工作流文件(用代码创建Xaml文件),并且动态加载运行所生成的工作流. 工作流生成后 在Xaml文件里的主要节点如下: 输入输出参数 <x:Mem ...
- java中使用fastjson、jackson、json-lib解析JSON-------------------妈妈再也不用担心JSON解析
1.fastjson引入包<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjso ...
- es6英文文档翻译
ECMA-262英文文档翻译,github地址: https://github.com/zhoushengmufc/es6 ECMA-262英文文档翻译,在线地址: http://zhoushengf ...