绑定到其它元素

<Grid> 
    <StackPanel> 
        <TextBox x:Name="textbox1" /> 
        <Label Content="{Binding ElementName=textbox1, Path=Text}" /> 
    </StackPanel>
</Grid>
 
绑定到静态资源
<Window.Resources> 
        <ContentControl x:Key="text">Hello, World!</ContentControl>
</Window.Resources>
<Grid> 
    <StackPanel> 
        <Label x:Name="label1" Content="{Binding Source={StaticResource text}}" /> 
    </StackPanel>
</Grid>
<STRONG>3. 绑定到自身</STRONG>
<Grid> 
    <StackPanel> 
        <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource Self}, Path=Name}" /> 
    </StackPanel>
</Grid>
绑定到指定类型的父元素
<Grid x:Name="Grid1"> 
        <StackPanel> 
            <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,  
                AncestorType={x:Type Grid}}, Path=Name}" /> 
        </StackPanel>
</Grid>
 
绑定到对象
public class Person  
   {  
       public string Name { get; set; }  
       public int Age { get; set; }  
   }
<StackPanel x:Name="stackPanel">  
        <StackPanel.DataContext>  
            <local:Person Name="Jack" Age="30"></local:Person>  
        </StackPanel.DataContext>  
        <TextBlock  Text="{Binding Path=Name}"></TextBlock>  
        <TextBlock Text="{Binding Path=Age}"></TextBlock>         
</StackPanel>

绑定到集合
public class Person  
    {  
        public string Name { get; set; }  
        public int Age { get; set; }  
    }

public class PersonList : ObservableCollection<Person>  
    { }
<Window.Resources>  
       <local:PersonList x:Key="person">  
           <local:Person Name="Jack" Age="30"></local:Person>  
           <local:Person Name="Tom" Age="32"></local:Person>  
       </local:PersonList>  
   </Window.Resources>  
   <StackPanel x:Name="stackPanel">  
       <ListBox  ItemsSource="{Binding Source={StaticResource ResourceKey=person}}" 
                  DisplayMemberPath="Name">             
       </ListBox> 
</StackPanel>
 
DataContext共享源
我们需要将同一资源绑定到多个 UI 元素上,很显然到处写 "{Binding Source={StaticResource person}}" 是件很繁琐且不利于修改的做法。WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。
<Window.Resources>  
        <local:PersonList x:Key="person">  
            <local:Person Name="Jack" Age="30"></local:Person>  
            <local:Person Name="Tom" Age="32"></local:Person>  
        </local:PersonList>  
    </Window.Resources>  
    <StackPanel x:Name="stackPanel" DataContext="{StaticResource person}">  
        <ListBox  ItemsSource="{Binding}" 
                   DisplayMemberPath="Name">             
        </ListBox>  
    </StackPanel>
 
使用XML作为Binding的源
XML:
<?xml version="1.0" encoding="utf-8" ?> 
<PersonList>  
  <Person Id="1">  
    <Name>Jack</Name>  
  </Person>  
  <Person Id="2">  
    <Name>Tom</Name>  
  </Person>  
  <Person Id="3">  
    <Name>Justin</Name>  
  </Person>  
  <Person Id="4">  
    <Name>David</Name>  
  </Person> 
</PersonList>

XAML:
<StackPanel>  
       <ListView x:Name="personListView">  
           <ListView.View>  
               <GridView>  
                   <GridViewColumn Header="Id" Width="100" 
                                    DisplayMemberBinding="{Binding XPath=@Id}"/>  
                   <GridViewColumn Header="Name" Width="100" 
                                    DisplayMemberBinding="{Binding XPath=Name}"/>  
               </GridView>  
           </ListView.View>  
       </ListView>     
       <Button Click="Button_Click">Load Data</Button>  
   </StackPanel>

private void Button_Click(object sender, RoutedEventArgs e)  
 {  
            XmlDocument xmlDocument = new XmlDocument();  
            xmlDocument.Load("Person.xml"); 
            XmlDataProvider xdp = new XmlDataProvider();  
            xdp.Document = xmlDocument;  
            xdp.XPath = @"/PersonList/Person"; 
  
            this.personListView.DataContext = xdp;  
            this.personListView.SetBinding(ListView.ItemsSourceProperty, new Binding());  
        }

WPF绑定方式的更多相关文章

  1. wpf中UserControl的几种绑定方式

    我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...

  2. WPF快速入门系列(4)——深入解析WPF绑定

    一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...

  3. WPF 绑定StaticResource到控件的方法

    原文:WPF 绑定StaticResource到控件的方法 资源文件内的属性能否直接通过绑定应用到控件?答案是肯定的. 比如,我们要直接把下面的<SolidColorBrush x:Key=&q ...

  4. WPF绑定各种数据源之object数据源

    一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...

  5. RegisterAttached 两种绑定方式

    RegisterAttached 含义:使用指定的属性名称.属性类型和所有者类型注册附加属性 绑定方式:C#绑定.WPF绑定 例:需求DataViewModel为DataView的VM层,在DataV ...

  6. WPF 绑定父类属性

    原文:WPF 绑定父类属性 1.绑定父控件的属性. <ContextMenu x:Key="ContextMenuColoum"> <MenuItem Heade ...

  7. WPF绑定(Binding)(4)

    什么是绑定(Binding)? 在winform中, 我们常常会用到各种类型的赋值, 例如: button1.Text="Hello"; label.Text="Hell ...

  8. 对比MFC资源文件谈谈WPF布局方式

    对比MFC资源文件谈谈WPF布局方式 MFC方式 对于传统的MFC基于UI的应用程序设计通常分两步走,首先是设计UI,使用的是RC文件,然后是代码文件,对RC文件进行操作,如下面Figure 1 的基 ...

  9. [Spring MVC] - SpringMVC的各种参数绑定方式

    SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") publi ...

随机推荐

  1. HTML-Audio/Video

    简介: 容器:不论是音频还是视频文件,实际上都是容器文件: 视频文件包含了音频轨道.视频轨道和其他一些元数据: 视频文件播放时,音频轨道和视频轨道是绑定在一起:元数据包含了该视频的封面.子标题.字幕等 ...

  2. ember.js:使用笔记6 子项目的前进与后退

    如下代码会根据model产生不同的table项,在进行其他设置后,一般是根据id来跳转到相应项目子项中: {{#each}} {{#link-to "tabls" this}}{{ ...

  3. 使用OUYA第一次启动OUYA

    使用OUYA第一次启动OUYA 1.4  使用OUYA 初次使用OUYA时,其启动以后的设置过程耗时较长,也比较繁琐,因此本节将会对其做个详细介绍,让读者的使用过程更加顺利些!好的开端总归是一个不错的 ...

  4. Bitnami Redmine安装和插件配置

    公司要进行敏捷开发管理,最后选择Redmine作为管理工具. 而Redmine本身的安装非常麻烦,要安装mysql,ruby,redmine,apach. 显然这不是一个偷懒的人应该做的,最后找到Bi ...

  5. DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi

    题目传送门 题意:问n长度的序列,找出长度m的上升子序列的方案数. 分析:这个问题就是问:dp[i][j] = sum (dp[i-1][k]) (1 <= k <= n, a[k] &l ...

  6. 数学+高精度 ZOJ 2313 Chinese Girls' Amusement

    题目传送门 /* 杭电一题(ACM_steps 2.2.4)的升级版,使用到高精度: 这次不是简单的猜出来的了,求的是GCD (n, k) == 1 最大的k(1, n/2): 1. 若n是奇数,则k ...

  7. Tri Tiling[HDU1143]

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

  9. BZOJ4143 [AMPPZ2014]The Lawyer

    Description Byteasar要制订m天的会议计划,一共有n场会议,第i场会议开始于第d[i]天的第a[i]秒,结束于第d[i]天的第b[i]秒. 对于每一天,请找出这一天的两场会议i,j, ...

  10. YUV YCbCr

    一,介绍 YUV是一种颜色空间 其中“Y”表示明亮度(Luminance或Luma),也就是灰阶值: 而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和 ...