WPF 绑定基础

简介:

Binding是Wpf的核心内容之一

1、下面是Binding基础的一个例子。

XAML:

<Grid>
     <TextBox x:Name= "myTextBox"  Height= "80"  HorizontalAlignment= "Left"  VerticalAlignment= "Top"  Width= "200" />
     <Button Height= "23"  HorizontalAlignment= "Left"  Margin= "38,89,0,0"  Name= "button1"  VerticalAlignment= "Top"  Width= "75"  Click= "button1_Click" >Button</Button>
</Grid>

     C# Code

public  partial  class  BindingTestxaml : Window
   {
       Customer cust;
       public  BindingTestxaml()
       {
           InitializeComponent();
           BindingTest();
       }
       private  void  BindingTest()
       {
           //准备数据源
           cust =  new  Customer();
          // cust.Name = "Tom";
 
          //准备Binding
           Binding bind = new  Binding();
           bind.Source = cust;
           bind.Path = new  System.Windows.PropertyPath( "Name" );
 
           //使用Binding连接数据源与Binding目标
           BindingOperations.SetBinding(myTextBox, TextBox.TextProperty, bind);
       }
 
       private  void  button1_Click( object  sender, RoutedEventArgs e)
       {
           cust.Name += " Tom" ;
       }
   }
 
   public  class  Customer:INotifyPropertyChanged
   {
       private  string  name;
 
       public  string  Name
       {
           get  { return  name; }
 
           set
           {
               this .name = value;
               //激发事件
               if  (PropertyChanged != null )
                   this .PropertyChanged.Invoke( this , new  PropertyChangedEventArgs( "Name" ));
 
           }
       }
 
       #region INotifyPropertyChanged 成员
 
       public  event  PropertyChangedEventHandler PropertyChanged;
 
       #endregion
   }

 当然上面的绑定也可以变成一句话的形式:如

//使用Binding连接数据源与Binding目标
  BindingOperations.SetBinding(myTextBox, TextBox.TextProperty, new  Binding( "Name" ) { Source= cust = new  Customer()});

 

2、没有Source的Binding——使用DataContext作为Binding的源,下面是一个简单的例子

先创建一个名为Sutdent的类,它有三个ID,Name,Age三个属性:

public  class  Student
   {
       public  string  ID{ get ; set ;}
       public  string  Name { get ; set ; }
       public  string  Age { get ; set ; }
   }

然后XAML创建程序UI:

<StackPanel Background= "LightBlue"  Margin= "0,118,0,0"  HorizontalAlignment= "Right"  Width= "201"  Height= "122"  VerticalAlignment= "Top" >
     <StackPanel.DataContext>
         <local:Student  ID= "10"  Name= "Work Hard Work Smart"  Age= "30" />
     </StackPanel.DataContext>
     <TextBox Height= "23"  HorizontalAlignment= "Center"  Margin= "10"   Name= "textBox10"  VerticalAlignment= "Top"  Text= "{Binding Path=ID}"   Width= "164"  />
     <TextBox Height= "23"  HorizontalAlignment= "Center"  Margin= "10"  Name= "textBox11"  VerticalAlignment= "Top"  Text= "{Binding Path=Name}"   Width= "164"  />
     <TextBox Height= "23"  HorizontalAlignment= "Center"   Name= "textBox12"  VerticalAlignment= "Top"  Text= "{Binding Path=Age}"   Width= "164"  />
</StackPanel>

三个TextBox的Text通过Binding获取值,Bindng指定了Path,没有指定Source。

 

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/09/13/2175278.html,如需转载请自行联系原作者

目录
相关文章
WPF—多重绑定和跨层级绑定
WPF—多重绑定和跨层级绑定
|
C# 数据格式 XML
WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)
原文:WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树) 一、WPF对象级(Window对象)资源的定义与查找 实例一: StaticR...
8052 0
|
9月前
|
C# 数据库
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
117 0
|
C#
WPF更新绑定字段
WPF更新绑定字段
73 0
|
前端开发 C#
WPF 之 数据与命令绑定 (MVVM方式)
WPF 之 数据与命令绑定 (MVVM方式)
160 0
WPF 之 数据与命令绑定 (MVVM方式)
|
C#
WPF 绑定父类属性
原文:WPF 绑定父类属性 1.绑定父控件的属性. 1 2 3 4 5 6 7 8 9 发现问题,父控件的属性如果是后期加载的,别如说Width或者Height不是固定的数值,那么绑定时没有效果的。
1233 0
|
C# 前端开发
wpf中的datagrid绑定操作按钮是否显示或者隐藏
如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了。 但是在wpf里不行。。网上搜索了好久才找到解决方法,原来只是binding那个visiable属性就行了,
6848 0
|
C# .NET 开发框架
WPF中使用ObjectDataProvider绑定方法
原文:WPF中使用ObjectDataProvider绑定方法    ObjectDataProvider提供了绑定任意.net类型的功能,具体功能如下: 1.ObjectDataProvider提供了绑定任意CLR类型的公嫩那个。
1245 0
|
C# 前端开发
WPF Image控件的绑定
原文:WPF Image控件的绑定      在我们平时的开发中会经常用到Image控件,通过设置Image控件的Source属性,我们可以加载图片,设置Image的source属性时可以使用相对路径也可以使用绝对路径,一般情况下建议使用绝对路径,类似于下面的形式Source="/Demo;Component/Images/Test.
1695 0
|
C#
WPF的5种绑定模式(mode)
原文:WPF的5种绑定模式(mode) WPF的绑定模式(mode)是枚举的 枚举值共有5个 1:OneWay(源变就更新目标属性) 2:TwoWay(源变就更新目标并且目标变就更新源) 3:OneTime(只根据源来设置目标,以后都不会变) 4:OneWayToSource(与OneWay相反) 5:Default(可以单向或双向,是靠被值定的源或目标是否有get或set来指定的) 所以绑定的话是需要选上面5个中的一个模式的,根据你的需要来选择,不选的话就会自动选择第五个的。
1098 0