原文: http://www.cnblogs.com/pasoraku/archive/2012/10/20/2732427.html

一、binding的一般步骤 1,准备数据源     数据源需要实现INotifyPropertyChanged接口     例如:

class Person : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private string name;
  public string Name
  {
    get { return name; }
    set
    {
      name = value;
      //触发事件
      if (PropertyChanged != null)
      {
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
      }
    }
  }
}

2,准备Binding对象

Person p = new Person("D-boy");
Binding binding = new Binding();
binding.Source = p;
binding.Path = new PropertyPath("Name");

3,用Binding对象将数据源和目标连结 假如在XAML处添加了一个TextBlock目标

<TextBlock x:Name="txtName"></TextBlock>

那么可以使用BindingOperations.SetBinding()方法将其进行binding了。

BindingOperations.SetBinding(txtName, TextBlock.TextProperty, binding);

也可以使用UI元素的基类FramworkElement封装的SetBinding函数

txtName.SetBinding(TextBlock.TextProperty, binding);

将上面两步结合在一起可以这样写

txtName.SetBinding(TextBlock.TextProperty, new Binding("Name") { Source=p});

二、控件间的Binding XAML处如下两个控件

<TextBox x:Name="txt1" />
<TextBlock x:Name="txt2" Text="{Binding Path=Text, ElementName=txt1}" />

如果要改变Binding源的触发事件,可以设置Binding的UpdateSourceTrigger属性为LostFocus、Explicit、PropertyChanged中的一种。
三、Binding的Path 1,Path的设置 如上例, XAML处为

<TextBlock x:Name="txt2" Text="{Binding Path=Text, ElementName=txt1}" />

或是

<TextBlock x:Name="txt2" Text="{Binding Text, ElementName=txt1}" />

相应的C#代码为

txt2.SetBinding(TextBlock.TextProperty, new Binding(){ Path=new PropertyPath("Value"),Source=txt1});

或是

txt2.SetBinding(TextBlock.TextProperty, new Binding("Text") { Source=txt1});

之类的... 2,Path还支持一路点下去~~ 比如

<TextBlock x:Name="txt2" Text="{Binding Text.Length, ElementName=txt1}" />
<TextBlock x:Name="txt2" Text="{Binding Text.[2], ElementName=txt1}" />

3,当数据源本身就是数据的时候,Path为. window添加命名空间

xmlns:sys="clr-namespace:System;assembly=mscorlib"

添加资源

<Window.Resources>
<sys:String x:Key="string">
string!!!!!
</sys:String>
</Window.Resources>

使用Binding

<TextBlock x:Name="txt2" Text="{Binding ., Source={StaticResource ResourceKey=string}}" />

精简为

<TextBlock x:Name="txt2" Text="{Binding Source={StaticResource ResourceKey=string}}" />

当然这样的情况,我就直接

<TextBlock x:Name="txt2" Text="{StaticResource ResourceKey=string}" />

WPF的Binding学习笔记(一)的更多相关文章

  1. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  2. ArcGIS API for Silverlight/WPF 2.1学习笔记(一)——精简版

    一.安装 1.Visual Studio: (1)Visual Studio 2010或Visual Web Developer Express 2010 (2)Silverlight 4 Tools ...

  3. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(四)

      七.Editing ArcGIS Server 10提供了: 通过feature service,在Web上编辑Feature layers的geographic data的功能. 通过geome ...

  4. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(五)

    2.Find示例代码 (1)xaml文件: //添加Symbol命名空间 xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbol ...

  5. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(一)

    源自:http://blog.163.com/zwx_gis/blog/static/32434435201122193611576/ (主页:http://blog.163.com/zwx_gis/ ...

  6. [转]WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组

    在第二十三节,我们使用CollectionView实现了对于绑定数据的导航,除导航功能外,还可以通过CollectionView对数据进行类似于DataView的排序.筛选等功能. 一.数据的排序: ...

  7. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(三)

    六.Feature Layer Feature Layer是一种特殊的Graphics layer(继承自Graphics layer),除了像Graphics layer一样包含和显示Graphic ...

  8. 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)

      五.Graphics layer 1.新增Graphics layer Graphics layer用于显示用户自定义绘制的点.线.面图形.使用时确保xaml文件中Graphics layer定义 ...

  9. [译]聊聊C#中的泛型的使用(新手勿入) Seaching TreeVIew WPF 可编辑树Ztree的使用(包括对后台数据库的增删改查) 字段和属性的区别 C# 遍历Dictionary并修改其中的Value 学习笔记——异步 程序员常说的「哈希表」是个什么鬼?

    [译]聊聊C#中的泛型的使用(新手勿入)   写在前面 今天忙里偷闲在浏览外文的时候看到一篇讲C#中泛型的使用的文章,因此加上本人的理解以及四级没过的英语水平斗胆给大伙进行了翻译,当然在翻译的过程中发 ...

随机推荐

  1. 从零开始学iPhone开发(3)——视图及控制器的使用

    上一节我们分别使用IB和代码建立了两个视图并且熟悉了一些控件.这一节我们需要了解视图和视图的切换. 在iOS编程中,框架提供了很多视图,比如UIView,UIImageView, UIWebView等 ...

  2. 怎么修改git提交过的内容

    git修改历史提交   Git使用amend选项提供了最后一次commit的反悔.但是对于历史提交呢,就必须使用rebase了. git rebase -i HEAD~3 表示要修改当前版本的倒数第三 ...

  3. Populating Tree Item With Record Group In Oracle Forms

    The below plsql program unit could be used in a WHEN-NEW-FORM-INSTANCE trigger to initially populate ...

  4. linux passwd文件解析

    #cat/etc/passwd root:x:::Superuser:/: daemon:x:::Systemdaemons:/etc: bin:x:::Ownerofsystemcommands:/ ...

  5. IE6兼容性问题及IE6常见bug详细汇总

    转载地址:http://www.jb51.net/css/76894.html 1.IE6怪异解析之padding与border算入宽高 原因:未加文档声明造成非盒模型解析 解决方法:加入文档声明&l ...

  6. wamp环境下phpmyadmin拒绝访问

    You don't have permission to access /phpmyadmin on this server. 找到 alias/phpmyadmin.conf  的配置文件 将 &l ...

  7. xml资源getStringArray(R.array.xxx)方法

    在res/values/下新建menu_names.xml 代码如下: <?xml version="1.0" encoding="utf-8"?> ...

  8. JS Map 和 List 的简单实现代码

    javascript中是没有map和list 结构的. 本篇文章是对在JS中Map和List的简单实现代码进行了详细的分析介绍,需要的朋友参考下 代码如下: /* * MAP对象,实现MAP功能 *  ...

  9. lhgdialog: iframe页面里面的,确定,关闭、取消按钮的操作

    lhgdialog: iframe页面里面的,确定,关闭.取消按钮的操作 如果你正在用lhgdialog,用他人iframe,或者 content:'url:http://www.baidu.com/ ...

  10. [讨论] 全新首发WIN7 32&64 WINSXS进一步精简批处理公测

    恶魔浮雕 发表于 2016-1-8 15:01:27 |https://www.itsk.com/forum.php?mod=viewthread&tid=362278&highlig ...