Binding TwoWay demo

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Data;


//System.ComponentModel 命名空间提供用于实现组件和控件运行时和设计时行为的类。
//此命名空间包括用于实现属性和类型转换器、绑定到数据源以及授权组件的基类和接口。


namespace SilverlightApplication2
{
public partial class text : UserControl
{
//一定要是公共的
User user = null;// new User();
public text()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
// System.ComponentModel.BindableAttribute
// Binding
user = new User();
user.Name = "TerryLee";
user.Address = "中国 天津";
//DataContext 数据上下文类型
//只要把user对象给控件的数据上下文即可
lblName.DataContext = user;
lblAddress.DataContext = user;

}

private void ChangeP_Click(object sender, RoutedEventArgs e)
{
user.Address = "china beijing";
user.Name = "muer";

}

private void Button_Click(object sender, RoutedEventArgs e)
{
this.CurrentPr.Text = " name:" + user.Name.ToString() + " Address:" + user.Address.ToString(); ;
}

}

//INotifyPropertyChanged:向客户端发出某一属性值已更改的通知。
public class User : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}

private string _address;
public string Address
{
get { return _address; }
set
{
_address = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Address"));
}
}
}
}
}

 

View Code
<UserControl x:Class="SilverlightApplication2.text"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth="400" Loaded="UserControl_Loaded">

<Grid x:Name="LayoutRoot" Background="#46461F">
<Grid.RowDefinitions>
<RowDefinition Height="160"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Width="100" Height="30" HorizontalAlignment="Left"
Content
="当前页面的属性值"
Grid.Row
="0"
Grid.Column
="0"
Click
="Button_Click"/>
<TextBlock Foreground="White" FontSize="18" Text="姓名:"
Grid.Row
="1" Grid.Column="0" HorizontalAlignment="Right"/>
<TextBox x:Name="lblName" FontSize="18"
Grid.Row
="1" Grid.Column="1" HorizontalAlignment="Left"
Text
="{Binding Name,Mode=TwoWay}"/>
<TextBlock Foreground="White" FontSize="18" Text="位置:"
Grid.Row
="2" Grid.Column="0" HorizontalAlignment="Right"/>
<TextBox x:Name="lblAddress" FontSize="18"
Grid.Row
="2" Grid.Column="1" HorizontalAlignment="Left"
Text
="{Binding Address,Mode=TwoWay}"/>
<Button Name="ChangeP" Click="ChangeP_Click"
DataContext
="{Binding}"
Content
="改变属性值 "
HorizontalAlignment
="Left"
Width
="78"
Height
="30"
Grid.Column
="1"
Grid.Row
="0"
></Button>
<TextBlock Foreground="White" FontSize="18"
x:Name
="CurrentPr"
Grid.Row
="4"
Grid.Column
="0"
HorizontalAlignment
="Left"
Grid.ColumnSpan
="2"/>
</Grid>
</UserControl>

 

 

总结:通过  Text="{Binding Address,Mode=TwoWay}"  这种方式,当我点击提交按钮的时候不需要写个方法去拿页面上所有元素的值,
只要拿到当前实例化的对象User就可以拿到这个页面的所有的值。这就是MVVM所带来的便捷,可以让开发人员少写很多代码。

posted @ 2012-03-08 12:00  红萝卜  阅读(667)  评论(0编辑  收藏  举报