无废话WPF系列9: Binding的源

本文详细介绍了WPF中各种数据绑定的方法,包括绑定到其他元素、静态资源、自身、指定类型的父元素、对象及集合等,并讲解了如何通过DataContext实现资源共享,最后还展示了如何使用XML作为数据源。

1. 绑定到其它元素

1
2
3
4
5
6
< Grid >
     < StackPanel >
         < TextBox  x:Name="textbox1" />
         < Label  Content="{Binding ElementName=textbox1, Path=Text}" />
     </ StackPanel >
</ Grid >
 

2. 绑定到静态资源

1
2
3
4
5
6
7
8
9
< 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 >
1
2
3
4
5
< Grid >
     < StackPanel >
         < Label  x:Name="label1" Content="{Binding RelativeSource={RelativeSource Self}, Path=Name}" />
     </ StackPanel >
</ Grid >

4. 绑定到指定类型的父元素

1
2
3
4
5
6
< Grid  x:Name="Grid1">
         < StackPanel >
             < Label  x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,
                 AncestorType={x:Type Grid}}, Path=Name}" />
         </ StackPanel >
</ Grid >

 

5. 绑定到对象 

1
2
3
4
5
public  class  Person
    {
        public  string  Name { get ; set ; }
        public  int  Age { get ; set ; }
    }
1
2
3
4
5
6
7
8
< 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 >

6. 绑定到集合

1
2
3
4
5
6
7
8
public  class  Person
     {
         public  string  Name { get ; set ; }
         public  int  Age { get ; set ; }
     }
 
     public  class  PersonList : ObservableCollection<Person>
     { }
1
2
3
4
5
6
7
8
9
10
11
< 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 >

 

7. DataContext共享源

我们需要将同一资源绑定到多个 UI 元素上,很显然到处写 "{Binding Source={StaticResource person}}" 是件很繁琐且不利于修改的做法。WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。

1
2
3
4
5
6
7
8
9
10
11
< 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 >

 

8. 使用XML作为Binding的源

XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
< 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 >

后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
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());
         }

image


本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2011/02/19/1958586.html如需转载请自行联系原作者


王德水

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值