——-
Windows Phone 7手机开发
.Net培训、期待与您交流! ——-
 
一、
WPF

,
对控件做数据
banding
,
可以方便的进行赋值和获得值的操作
:


例如一
:

<
Grid
> 

                
<
Slider
Name
=”Slider1″
HorizontalAlignment
=”Left”
Margin
=”38,55,0,0″
VerticalAlignment
=”Top”
Height
=”19″
Width
=”227″
/> 

                
<
TextBox
Text
=”{Binding Value,ElementName=Slider1}”
HorizontalAlignment
=”Left”
Height
=”23″
Margin
=”38,101,0,0″
TextWrapping
=”Wrap”
VerticalAlignment
=”Top”
Width
=”120″
/>


</
Grid
>

显示结果
:(
可以通过
slider
来滑动,控制
textbox
值的显示
)
黑马程序员---wpf学习笔记四---banding的那些事
例二

为控件设置数据源
:
 
1
.a  New
一个类的实例,给 要绑定的控件设定
DataContext
  
private
void Window_Loaded(
object sender, RoutedEventArgs e)


InBlock.gif  {


InBlock.gif                        Person person =
new Person();


InBlock.gif                        person.Age = 22;


InBlock.gif                        person.Name =
“Charles”;


InBlock.gif                        tbAge.DataContext = person;


InBlock.gif                        tbName.DataContext = person;


InBlock.gif }


InBlock.gif 
public
class Person


InBlock.gif        { 

                
public
int Age { set; get; }


InBlock.gif             
public
string Name { set; get; }


InBlock.gif        }

 
1
.
b
   XAML
中设置数据绑定的控件属性

    
<
TextBox
Name
=”tbName”
ToolTip
=”{Binding Name}”
HorizontalAlignment
=”Left”
Height
=”23″
Margin
=”80,162,0,0″
TextWrapping
=”Wrap”
Text
=”{Binding Name}”
VerticalAlignment
=”Top”
Width
=”120″
/>

                
<
TextBox
Name
=”tbAge”
ToolTip
=”{Binding Age}”
HorizontalAlignment
=”Left”
Height
=”23″
Margin
=”80,207,0,0″
TextWrapping
=”Wrap”
Text
=”{Binding Age}”
VerticalAlignment
=”Top”
Width
=”120″
/>

                        
<
TextBlock
Text
=”姓名”
Width
=”30″
Margin
=”39,163,434,125″
/>

                        
<
TextBlock
Margin
=”38,207,435,81″
Text
=”年龄”
Width
=”30″
/>



显示效果:
 
黑马程序员---wpf学习笔记四---banding的那些事
   
      但是
,
此种做法
,
无法将从后台变化的值在界面体现
;
如果需要后台对象值变化
,
前台也变化
,
就使用【
INotifyPropertyChanged
】”属性变化通知”机制来实现
.
  2.a

修改类的定义
(
注意需要引入
System.ComponentModel
)
定义的类,需要继承
INotifyPropertyChanged
,并实现该接口方法,如下:
引入
:
using
System.ComponentModel;

InBlock.gif
class Person : INotifyPropertyChanged


InBlock.gif     {


InBlock.gif                
private
int age;


InBlock.gif                
private
string name;


InBlock.gif                
public
int Age


InBlock.gif                {


InBlock.gif                        get {
return age; }


InBlock.gif                        set


InBlock.gif                        {


InBlock.gif                            
this.age = value;


InBlock.gif                                
if (PropertyChanged !=
null)


InBlock.gif                                {


InBlock.gif                                        PropertyChanged(
this,
new PropertyChangedEventArgs(
“Age”));


InBlock.gif                                }


InBlock.gif                        }


InBlock.gif                }


InBlock.gif                
public
string Name


InBlock.gif                {


InBlock.gif                        get {
return name; }


InBlock.gif                        set {


InBlock.gif                                
this.name = value;


InBlock.gif                                
if (PropertyChanged !=
null)


InBlock.gif                                {


InBlock.gif                                        PropertyChanged(
this,
new PropertyChangedEventArgs(
“Name”));


InBlock.gif                                }                                        


InBlock.gif                        }


InBlock.gif                }


InBlock.gif                
public
event PropertyChangedEventHandler PropertyChanged;


InBlock.gif}

   
 
其他步骤同上述
1.a\1.b,
为看到效果,可以添加一个按钮来实现!
InBlock.gif                    
private
void btAge_Click(
object sender, RoutedEventArgs e)


InBlock.gif                {


InBlock.gif                        person.Age++;


InBlock.gif                }


截图:
黑马程序员---wpf学习笔记四---banding的那些事
二、给单个控件逐一赋值还是给所有控件共有的父控件赋值(进行数据
banding
)之争:

    
对控件上下文
(
DataContext
)
的指定
,
第一种
:
可以直接指定给控件
,
缺点是当控件多了
,
代码量相对来说就多来了
;
第二种
:
指定给其父控件
,
当不给子控件指定
DataContext

,
默认使用父控件的上下文
,
相对来说
,
代码简约
.
如下
:
采用第一种
:
InBlock.gif                        Person person =
new Person();


InBlock.gif                        person.Age = 22;


InBlock.gif                        person.Name =
“Charles”;


InBlock.gif                        tbAge.DataContext = person;


InBlock.gif                        tbName.DataContext = person;


采用第二种
:
InBlock.gifPerson person =
new Person();


InBlock.gif
this.DataContext = person;(注意:
this,指代当前窗体,也可以使用当前需要绑定上下文的共有父控件,如grid1.DataContext=person)
 
——-
Windows Phone 7手机开发
.Net培训、期待与您交流! ——-