1.把控件作为Binding源与Binding标记扩展
<TextBox x:Name="textBox1" Text="{Binding Path=Value, ElementName=slider1}" />

<TextBox x:Name="textBox1" Text="{Binding Value, ElementName=slider1}" />

<Slider x:Name="slider1" Maximum="100" Minimum="0" Margin="5" />

Binding还支持多级路径
<TextBox x:Name="textBox1" Text="{Binding Path=Text.Length, ElementName=textBox1,Mode=OneWay}" />

2.控制Binding的方向及数据更新
Binding的数据流向的属性BindingMode[TwoWay, OneWay, OnTime, OneWayToSource, Default]

3.没有Path的Binding
<StackPannel.Resources>
   <sys:String x:Key="myString">喜欢美女</sys:String>
<StackPanel.Resources>

<TextBlock x:Name="textBlock1" Text="{Binding Paht=.,Source={StaticResource ResourceKey=myString}}"  />

<TextBlock x:Name="textBlock1" Text="{Binding Paht=.,Source={StaticResource ResourceKey=myString}}"  />

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

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

注意:
a.c#代码里进行绑定字符串时是不能省略path的.
string myString="fuckfuck";
this.textBlock1.setBinding(TextBlock.TextProperty,new Binding("."){Source=myString});
b.XAML代码里的这个path和"."可省略

4.使用DataContext作为Binding的源
public class Student
{
   public int Id {get;set;}
   public string Name {get;set;}
   public int Age {get;set;}
}
xmlns:local="clr-namespace:WpfApplication1"  //命名空间下

<StackPanel>
   <StackPanel.DataContext>
        <local:Student Id="6" Age="29" Name="Tim" />   //数据
  </StackPanel.DataContext>
       <TextBox Text="{Binding Path=Id}" Margin="5" />
       <TextBox Text="{Binding Path=Name}" Margin="5" />
       <TextBox Text="{Binding Path=Age}" Margin="5" />
</StackPanel>

<Grid DataContext="6">
  <Grid>
       <Grid>
              <Button x:Name="btn" Content="OK" Click="btn_Click" />
       </Grid>
  </Grid>
</Grid>
private void btn_Click(object sender,RoutedEventArgs e)
{
     MessageBox.Show(btn.DataContext.ToString()); //会显示6,因为属性值沿UI元素树向下传递
}

注:其实DataContext是一个依赖属性,会向内层控件传递

<StackPanel>
   <StackPanel.DataContext>
        <sys:String>Hello DataContext </sys:string>   //数据
  </StackPanel.DataContext>
       <TextBox Text="{Binding}" Margin="5" />
</StackPanel>

5.使用集合对象进行ItemsSource绑定
wpf中的列表式控件是派生自ItemsControl类,自然也就继承了ItemSource这个属性,每个ItemsControl的派生类都具有自己对应的条目容器(Item Container),如ListBox的条目容器是ListBoxItem,ComboxBox的条目容器是ComboBoxItem

this.listBoxStudents.ItemsSource=stuList; //设置绑定数据源
this.listBoxStudents.DisplayMemberPath="Name"; //绑定要显示的属性

<ListBox x:Name="listBoxStudents">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Id}" Width="30" />
      </StackPanel>
        </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

6使用ADO.NET对象作为Binding的源
DataTable dt=this.GetData();
this.listBoxStudents.DisplayMemberPath="Name";
this.listBoxStudents.ItemsSource=dt.DefaultView;

<ListView x:Name="listViewStudents" >
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Id}" />
      <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Name}" />
      <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Age}" />
    </GridView>
  </ListView.View>
</ListView>

注:ListView是ListBox的派生类,GridView是ViewBase的派生类,ListView的View属性是一个ViewBase类型的对象,目前只有一个GridView可用于ListView。

7.XML数据作为Binding的源
假设xml文件存于D:\RawData.xml
<?xml version="1.0" encoding="utf-8">
<StudentList>
  <Student Id="1">
    <Name>Tim</Name>
  </Student>

  <Student Id="2">
    <Name>Tom</Name>
  </Student>
<StudentList>

<ListView x:Name="listViewStudents" >
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding XPath=@Id}" />
      <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding XPath=@Name}" />
      <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding XPath=@Age}" />
    </GridView>
  </ListView.View>
</ListView>

后台读取xml数据
XmlDocument doc=new XmlDocument();
doc.Load(@"D:\RawData.xml");

XmlDataProvider xdp=new XmlDataProvider();
xdp.Document=doc;
xdp.XPath=@"/StudentList/Student";

XmlDataProvider xdp=new XmlDataProvider();
xdp.Source=new Uri(@"D:\RawData.xml");
xdp.XPath=@"/StudentList/Student";

this.listViewStudents.DataContext=xdp;
this.listViewStudents.SetBinding(ListView.ItemsSourceProperty,new Binding());

LINQ查询绑定
this.listViewStudents.ItemsSource=from stu in stuList where stu.Name.StartsWith("T") select stu;

DataTable dt=this.GetDataTable();
this.listViewStudents.ItemsSource=
  from row in dt.Rows.Cast<DataRow>()
  where Convert.ToString(row["Name"]).StartsWith("T")
  select new Student()
  {
           Id=int.Parse(row["Id"].ToString()),
    Name=row["Name"].ToString(),
    Age=int.Parse(row["Age"].ToString())
  };

this.listViewStudents.ItemsSource=
  from element in xdoc.Descendants("Student")
  where element.Attribute("Name").Value.StartsWith("T")
  select new Student()
  {
           Id=int.Parse(element.Attribute("Id").Value),
     Name=element.Attribute("Name").Value,
     Age=int.Parse(element.Attribute("Age").Value),
  };

深入浅出-Binding的源与路径的更多相关文章

  1. Binding的源与路径

    1.把控件作为Binding的源 例子:拖动Slider,输入框中的值也会跟着改变,或在输入框中输入数值,滑动条也会自动移动 <Window x:Class="把控件作为Binding ...

  2. WPF 基础 - Binding 的源与路径

    1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...

  3. 【深入浅出jQuery】源码浅析2--奇技淫巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  4. 【深入浅出jQuery】源码浅析2--使用技巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  5. 【深入浅出jQuery】源码浅析--整体架构

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  6. (转)【深入浅出jQuery】源码浅析2--奇技淫巧

    [深入浅出jQuery]源码浅析2--奇技淫巧 http://www.cnblogs.com/coco1s/p/5303041.html

  7. 浅谈 qmake 之 shadow build(将源码路径和构建路径分开,一套源码要分别用msvc2008、msvc2008、mingw分别编译又不互相干扰)

    shadow build shadow build 是什么东西?就是将源码路径和构建路径分开(也就是生成的makefile文件和其他产物都不放到源码路径),以此来保证源码路径的清洁. 这不是qmake ...

  8. 【深入浅出jQuery】源码浅析--整体架构(转)

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  9. 相辅相成的求最单源短路径算法:(SPFA& dijkstra)

    引用一位老oier的话: 一道题如果边权没有负数,那么一定是在卡SPFA.这时候就用到了堆优化的Dijkstra; 写在前面: 多打代码! 最好都掌握,灵活变通 SPFA: 主要用于稀疏图和有负权边的 ...

随机推荐

  1. 前端 javascript 写代码方式

    javascript 和python一样可以用终端写代码 写Js代码: - html文件中编写 - 临时,浏览器的终端 console  

  2. SQL事务回滚 写法(转)

    以下是SQL 回滚的语句:方案一:SET   XACT_ABORT   ON--如果产生错误自动回滚GOBEGIN   TRANINSERT   INTO   A   VALUES   (4)INSE ...

  3. js老生常谈之this,constructor ,prototype

    前言 javascript中的this,constructor ,prototype,都是老生常谈的问题,深入理解他们的含义至关重要.在这里,我们再来复习一下吧,温故而知新! this this表示当 ...

  4. 简明python教程八----输入/输出

    通过创建一个file类的对象来打开一个文件,分别使用file类的read.readline或write方法来读写文件. 最后调用一个close方法来告诉Python我们完成了对文件的使用. poem= ...

  5. 在 Sublime Text 2 中编译和运行 Java 程序,以及输出中文出错问题解决办法

    Sublime Text 2 是我最喜欢用来编码的文本编辑器,如果你尝试使用后相信你也会喜欢上它的.在这篇文章中我们将讨论如何在 Sublime Text 2 中编译和运行 Java 程序. 第一步: ...

  6. gcc __attribute__

    GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和 ...

  7. Xcode控制台命令

    命令 解释 break NUM 在指定的行上设置断点 bt 显示所有的调用栈帧,该命令可用来显示函数的调用顺序 clear 删除设置在特定源文件.特定行上的断点,其用法为:clear FILENAME ...

  8. delphi pchar 指针错误

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 var P: Pchar; //P 是指针 CSize: Cardinal; ...

  9. EntityFramework 6 开篇

    本系列文章主要来讲解理解以及怎样使用EntityFramework,写这个系列主要是因为部门里面准备来使用EF,为了让大家一起来学习,我每天发布1-2篇文章让大家一块参与学习.之前一直写在有道云笔记里 ...

  10. CCF 权限查询(模拟)

    试题编号: 201612-3 试题名称: 权限查询 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 授权 (authorization) 是各类业务系统不可缺少的组成部分,系统 ...